From cf8843f39195d003c6f3727d1de9357b5215c4a0 Mon Sep 17 00:00:00 2001 From: NIKEA-SOFT Date: Wed, 18 May 2022 21:36:07 +0300 Subject: [PATCH] Replace macros and fixes memory leak and possible UB --- ai/default/aiplayer.cpp | 3 +- ai/default/aisettler.cpp | 10 +- ai/default/aiunit.cpp | 5 +- ai/default/daicity.cpp | 3 +- ai/tex/texai.c | 13 +-- ai/tex/texaicity.c | 10 +- ai/tex/texaiplayer.c | 14 ++- ai/threaded/taicity.c | 2 +- ai/threaded/taiplayer.c | 11 +- ai/threaded/threadedai.c | 13 +-- client/attribute.cpp | 5 +- client/citydlg_common.cpp | 3 +- client/governor.cpp | 6 +- client/gui-qt/chatline.cpp | 2 +- client/gui-qt/citydlg.cpp | 10 +- client/gui-qt/dialogs.cpp | 9 +- client/gui-qt/icons.cpp | 12 ++- client/gui-qt/idlecallback.cpp | 6 +- client/gui-qt/mapview.cpp | 6 +- client/gui-qt/minimap.cpp | 6 +- client/gui-qt/page_game.cpp | 3 +- client/gui-qt/page_network.cpp | 6 +- client/gui-qt/sciencedlg.cpp | 17 ++-- client/gui-qt/shortcuts.cpp | 6 +- client/gui-qt/unitreport.cpp | 6 +- client/gui-qt/unitselect.cpp | 7 +- client/luascript/script_client.cpp | 6 +- client/mapview_common.cpp | 2 +- client/messagewin_common.cpp | 3 +- client/options.cpp | 52 ++++++---- client/packhand.cpp | 16 +-- client/reqtree.cpp | 4 +- client/servers.cpp | 6 +- client/tilespec.cpp | 23 +++-- client/update_queue.cpp | 6 +- client/voteinfo.cpp | 6 +- common/achievements.cpp | 4 +- common/actions.cpp | 3 +- common/aicore/cm.cpp | 9 +- common/aicore/path_finding.cpp | 8 +- common/citizens.cpp | 5 +- common/city.cpp | 9 +- common/clientutils.cpp | 10 +- common/extras.cpp | 18 ++-- common/game.cpp | 21 ++-- common/government.cpp | 9 +- common/helpdata.cpp | 15 +-- common/idex.cpp | 6 +- common/improvement.cpp | 3 +- common/map.cpp | 23 ++--- common/mapimg.cpp | 5 +- common/multipliers.cpp | 3 +- common/networking/connection.cpp | 16 ++- common/networking/packets.cpp | 17 ++-- common/player.cpp | 13 +-- common/player.h | 2 +- common/scriptcore/luascript.cpp | 3 +- common/scriptcore/luascript_signal.cpp | 4 +- common/specialist.cpp | 3 +- common/style.cpp | 6 +- common/team.cpp | 16 +-- common/tech.cpp | 14 +-- common/terrain.cpp | 14 +-- common/tile.cpp | 2 +- common/traderoutes.cpp | 3 +- common/unit.cpp | 12 ++- common/unittype.cpp | 26 +++-- server/advisors/advdata.cpp | 28 ++++-- server/advisors/infracache.cpp | 3 +- server/cityhand.cpp | 5 +- server/citytools.cpp | 21 ++-- server/cityturn.cpp | 24 +++-- server/diplomats.cpp | 3 +- server/edithand.cpp | 10 +- server/fcdb.cpp | 6 +- server/gamehand.cpp | 3 +- server/generator/mapgen.cpp | 20 ++-- server/generator/mapgen_utils.cpp | 3 +- server/generator/startpos.cpp | 12 ++- server/generator/temperature_map.cpp | 3 +- server/notify.cpp | 16 ++- server/plrhand.cpp | 6 +- server/report.cpp | 12 ++- server/rscompat.cpp | 3 +- server/ruleset.cpp | 133 +++++++++++++++++++------ server/savegame/savecompat.cpp | 8 +- server/savegame/savegame2.cpp | 40 ++++---- server/savegame/savegame3.cpp | 48 +++++---- server/scripting/script_server.cpp | 3 +- server/sernet.cpp | 3 +- server/stdinhand.cpp | 3 +- server/unittools.cpp | 3 +- tools/ruledit/tab_nation.cpp | 5 +- tools/ruleutil/rulesave.cpp | 3 +- utility/fcintl.cpp | 6 +- utility/genhash.cpp | 1 + utility/log.cpp | 7 +- utility/registry_ini.cpp | 3 +- utility/section_file.cpp | 8 +- utility/shared.cpp | 12 ++- utility/timing.cpp | 6 +- 101 files changed, 681 insertions(+), 409 deletions(-) diff --git a/ai/default/aiplayer.cpp b/ai/default/aiplayer.cpp index fefefc33cb..7e3cbc0751 100644 --- a/ai/default/aiplayer.cpp +++ b/ai/default/aiplayer.cpp @@ -54,7 +54,8 @@ void dai_player_free(struct ai_type *ait, struct player *pplayer) if (player_data != nullptr) { player_set_ai_data(pplayer, ait, nullptr); - FC_FREE(player_data); + delete player_data; + player_data = nullptr; } } diff --git a/ai/default/aisettler.cpp b/ai/default/aisettler.cpp index 88941be965..5a892fa3cd 100644 --- a/ai/default/aisettler.cpp +++ b/ai/default/aisettler.cpp @@ -247,7 +247,7 @@ static void cityresult_destroy(struct cityresult *result) if (result != nullptr) { if (result->tdc_hash != nullptr) { for (const auto *ptdc : qAsConst(*result->tdc_hash)) { - NFCPP_FREE(ptdc); + delete[] ptdc; } delete result->tdc_hash; } @@ -379,7 +379,7 @@ static struct cityresult *cityresult_fill(struct ai_type *ait, } if (result->tdc_hash->contains(cindex)) { - NFCPP_FREE(result->tdc_hash->value(cindex)); + delete[] result->tdc_hash->value(cindex); } result->tdc_hash->insert(cindex, ptdc); } @@ -540,7 +540,7 @@ static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex, #endif // FREECIV_DEBUG if (ai->settler->tdc_hash->contains(tindex)) { - NFCPP_FREE(ai->settler->tdc_hash->value(tindex)); + delete[] ai->settler->tdc_hash->value(tindex); } ai->settler->tdc_hash->insert(tindex, ptdc); } @@ -1196,7 +1196,7 @@ void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer) #endif // FREECIV_DEBUG for (const auto *ptdc : qAsConst(*ai->settler->tdc_hash)) { - NFCPP_FREE(ptdc); + delete[] ptdc; } ai->settler->tdc_hash->clear(); @@ -1213,7 +1213,7 @@ void dai_auto_settler_free(struct ai_plr *ai) fc_assert_ret(ai != nullptr); if (ai->settler) { - NFC_FREE(ai->settler->tdc_hash); + delete ai->settler->tdc_hash; delete[] ai->settler; } ai->settler = nullptr; diff --git a/ai/default/aiunit.cpp b/ai/default/aiunit.cpp index ec4659c594..1f5f559940 100644 --- a/ai/default/aiunit.cpp +++ b/ai/default/aiunit.cpp @@ -3260,7 +3260,7 @@ void dai_unit_init(struct ai_type *ait, struct unit *punit) /* Make sure that contents of unit_ai structure are correctly initialized, * if you ever allocate it by some other mean than new */ - struct unit_ai *unit_data = new unit_ai[1](); + struct unit_ai *unit_data = new unit_ai{}; unit_data->done = false; unit_data->cur_pos = nullptr; @@ -3301,7 +3301,8 @@ void dai_unit_close(struct ai_type *ait, struct unit *punit) if (unit_data != nullptr) { unit_set_ai_data(punit, ait, nullptr); - FCPP_FREE(unit_data); + delete unit_data; + unit_data = nullptr; } } diff --git a/ai/default/daicity.cpp b/ai/default/daicity.cpp index b959cc549e..0f56a9dd7e 100644 --- a/ai/default/daicity.cpp +++ b/ai/default/daicity.cpp @@ -1154,7 +1154,8 @@ void dai_city_free(struct ai_type *ait, struct city *pcity) if (city_data != nullptr) { adv_deinit_choice(&(city_data->choice)); city_set_ai_data(pcity, ait, nullptr); - FCPP_FREE(city_data); + delete[] city_data; + city_data = nullptr; } } diff --git a/ai/tex/texai.c b/ai/tex/texai.c index 76375843bd..ef359d73cb 100644 --- a/ai/tex/texai.c +++ b/ai/tex/texai.c @@ -70,7 +70,8 @@ static void texai_module_close(void) { TEXAI_AIT; - FC_FREE(ait->private); + delete ait->private; + ait->private = nullptr; } /**********************************************************************/ /** @@ -568,8 +569,6 @@ const char *fc_ai_tex_capstr(void) { return FC_AI_MOD_CAPSTR; } **************************************************************************/ bool fc_ai_tex_setup(struct ai_type *ai) { - struct dai_private_data *private; - if (!has_thread_cond_impl()) { qCritical(_("This Freeciv21 compilation has no full threads " "implementation, tex ai cannot be used.")); @@ -578,12 +577,10 @@ bool fc_ai_tex_setup(struct ai_type *ai) qstrncpy(ai->name, "tex", sizeof(ai->name)); -private - = fc_malloc(sizeof(struct dai_private_data)); -private - ->contemplace_workers = + ai->private = new struct dai_private_data { + }; + ai->private->contemplace_workers = false; /* We use custom code to set worker want and type */ - ai->private = private; texai_init_self(ai); diff --git a/ai/tex/texaicity.c b/ai/tex/texaicity.c index 8ee5b1b24b..b734288a08 100644 --- a/ai/tex/texaicity.c +++ b/ai/tex/texaicity.c @@ -561,7 +561,7 @@ void texai_req_worker_task_rcv(struct texai_req *req) struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0); if (ptask == NULL) { - ptask = fc_malloc(sizeof(struct worker_task)); + ptask = new worker_task{}; worker_task_init(ptask); worker_task_list_append(pcity->task_reqs, ptask); } @@ -585,7 +585,8 @@ void texai_req_worker_task_rcv(struct texai_req *req) **************************************************************************/ void texai_city_alloc(struct ai_type *ait, struct city *pcity) { - struct texai_city *city_data = fc_calloc(1, sizeof(struct texai_city)); + struct texai_city *city_data = new struct texai_city { + }; city_data->defai.building_wait = BUILDING_WAIT_MINIMUM; adv_init_choice(&(city_data->defai.choice)); @@ -602,7 +603,8 @@ void texai_city_free(struct ai_type *ait, struct city *pcity) if (city_data != NULL) { adv_deinit_choice(&(city_data->defai.choice)); - city_set_ai_data(pcity, ait, NULL); - FC_FREE(city_data); + city_set_ai_data(pcity, ait, nullptr); + delete city_data; + city_data = nullptr; } } diff --git a/ai/tex/texaiplayer.c b/ai/tex/texaiplayer.c index ea0954fa6b..5b30e479b9 100644 --- a/ai/tex/texaiplayer.c +++ b/ai/tex/texaiplayer.c @@ -251,7 +251,8 @@ static enum texai_abort_msg_class texai_check_messages(struct ai_type *ait) ret_abort = new_abort; } - FC_FREE(msg); + delete msg; + msg = nullptr; texaimsg_list_allocate_mutex(exthrai.msgs_to.msglist); } @@ -265,7 +266,7 @@ static enum texai_abort_msg_class texai_check_messages(struct ai_type *ait) **************************************************************************/ void texai_player_alloc(struct ai_type *ait, struct player *pplayer) { - struct texai_plr *player_data = fc_calloc(1, sizeof(struct texai_plr)); + struct texai_plr *player_data = new texai_plr{}; player_set_ai_data(pplayer, ait, player_data); @@ -288,7 +289,8 @@ void texai_player_free(struct ai_type *ait, struct player *pplayer) if (player_data != NULL) { player_set_ai_data(pplayer, ait, NULL); unit_list_destroy(player_data->units); - FC_FREE(player_data); + delete player_data; + player_data = nullptr; } } @@ -378,7 +380,8 @@ void texai_refresh(struct ai_type *ait, struct player *pplayer) if (pcity != NULL && city_owner(pcity) == req->plr) { adv_choice_copy(&(def_ai_city_data(pcity, ait)->choice), &(choice_req->choice)); - FC_FREE(choice_req); + delete choice_req; + choice_req = nullptr; } } break; case TEXAI_REQ_TURN_DONE: @@ -386,7 +389,8 @@ void texai_refresh(struct ai_type *ait, struct player *pplayer) break; } - FC_FREE(req); + delete req; + req = nullptr; texaireq_list_allocate_mutex(exthrai.reqs_from.reqlist); } diff --git a/ai/threaded/taicity.c b/ai/threaded/taicity.c index 7dba819058..9f9b0aed5b 100644 --- a/ai/threaded/taicity.c +++ b/ai/threaded/taicity.c @@ -502,7 +502,7 @@ void tai_req_worker_task_rcv(struct tai_req *req) struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0); if (ptask == NULL) { - ptask = fc_malloc(sizeof(struct worker_task)); + ptask = new worker_task{}; worker_task_init(ptask); worker_task_list_append(pcity->task_reqs, ptask); } diff --git a/ai/threaded/taiplayer.c b/ai/threaded/taiplayer.c index f1840eb482..821eae2368 100644 --- a/ai/threaded/taiplayer.c +++ b/ai/threaded/taiplayer.c @@ -153,7 +153,8 @@ static enum tai_abort_msg_class tai_check_messages(struct ai_type *ait) ret_abort = new_abort; } - FC_FREE(msg); + delete msg; + msg = nullptr; thrai.msgs_to.msglist.lock(); } @@ -167,7 +168,7 @@ static enum tai_abort_msg_class tai_check_messages(struct ai_type *ait) **************************************************************************/ void tai_player_alloc(struct ai_type *ait, struct player *pplayer) { - struct tai_plr *player_data = fc_calloc(1, sizeof(struct tai_plr)); + struct tai_plr *player_data = new tai_plr{}; player_set_ai_data(pplayer, ait, player_data); @@ -187,7 +188,8 @@ void tai_player_free(struct ai_type *ait, struct player *pplayer) if (player_data != NULL) { player_set_ai_data(pplayer, ait, NULL); - FC_FREE(player_data); + delete player_data; + player_data = nullptr; } } @@ -259,7 +261,8 @@ void tai_refresh(struct ai_type *ait, struct player *pplayer) break; } - FC_FREE(req); + delete req; + req = nullptr; thrai.reqs_from.reqlist.lock(); } diff --git a/ai/threaded/threadedai.c b/ai/threaded/threadedai.c index bcf5667d9b..047026cda1 100644 --- a/ai/threaded/threadedai.c +++ b/ai/threaded/threadedai.c @@ -69,7 +69,8 @@ static void tai_module_close(void) { TAI_AIT; - FC_FREE(ait->private); + delete ait->private; + ait->private = nullptr; } /**********************************************************************/ /** @@ -563,8 +564,6 @@ const char *fc_ai_threaded_capstr(void) { return FC_AI_MOD_CAPSTR; } **************************************************************************/ bool fc_ai_threaded_setup(struct ai_type *ai) { - struct dai_private_data *private; - if (!has_thread_cond_impl()) { qCritical(_("This Freeciv21 compilation has no full threads " "implementation, threaded ai cannot be used.")); @@ -573,11 +572,9 @@ bool fc_ai_threaded_setup(struct ai_type *ai) qstrncpy(ai->name, "threaded", sizeof(ai->name)); -private - = fc_malloc(sizeof(struct dai_private_data)); -private - ->contemplace_workers = true; - ai->private = private; + ai->private = new struct dai_private_data { + }; + ai->private->contemplace_workers = true; tai_init_self(ai); diff --git a/client/attribute.cpp b/client/attribute.cpp index 14c6cc6e3d..74a735fb56 100644 --- a/client/attribute.cpp +++ b/client/attribute.cpp @@ -301,11 +301,12 @@ void attribute_flush() } if (pplayer->attribute_block.data) { - free(pplayer->attribute_block.data); + delete[] pplayer->attribute_block.data; pplayer->attribute_block.data = nullptr; } - serialize_hash(attribute_hash, &pplayer->attribute_block.data, + serialize_hash(attribute_hash, + reinterpret_cast(&pplayer->attribute_block.data), &pplayer->attribute_block.length); send_attribute_block(pplayer, &client.conn); } diff --git a/client/citydlg_common.cpp b/client/citydlg_common.cpp index 4192f1859d..a2b2a239dd 100644 --- a/client/citydlg_common.cpp +++ b/client/citydlg_common.cpp @@ -512,7 +512,8 @@ static void fc__attribute((__format__(__printf__, 5, 6))) fc_vsnprintf(buf + qstrlen(buf), bufsz - qstrlen(buf), totalfmt, args); va_end(args); - FC_FREE(sum); + delete sum; + sum = nullptr; } /** diff --git a/client/governor.cpp b/client/governor.cpp index 708aa407bb..f25b17ea17 100644 --- a/client/governor.cpp +++ b/client/governor.cpp @@ -100,7 +100,11 @@ class cma_yoloswag { Q_GLOBAL_STATIC(cma_yoloswag, gimb) // deletes governor -void governor::drop() { NFCN_FREE(m_instance); } +void governor::drop() +{ + delete m_instance; + m_instance = nullptr; +} governor::~governor() = default; diff --git a/client/gui-qt/chatline.cpp b/client/gui-qt/chatline.cpp index 5ea3fb2dbe..c1bac8b4cc 100644 --- a/client/gui-qt/chatline.cpp +++ b/client/gui-qt/chatline.cpp @@ -224,7 +224,7 @@ void chat_input::send() void chat_input::chat_word_list_changed(const QStringList &word_list) { cmplt = completer(); - NFC_FREE(cmplt); + delete cmplt; cmplt = new QCompleter(word_list); cmplt->setCaseSensitivity(Qt::CaseInsensitive); cmplt->setCompletionMode(QCompleter::InlineCompletion); diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp index 327738de7a..a6950493a5 100644 --- a/client/gui-qt/citydlg.cpp +++ b/client/gui-qt/citydlg.cpp @@ -107,7 +107,7 @@ progress_bar::progress_bar(QWidget *parent) : QProgressBar(parent) */ progress_bar::~progress_bar() { - NFC_FREE(pix); + delete pix; delete sfont; } @@ -137,7 +137,7 @@ void progress_bar::set_pixmap(struct universal *target) } else { sprite = get_building_sprite(tileset, target->value.building); } - NFC_FREE(pix); + delete pix; if (sprite == nullptr) { pix = nullptr; return; @@ -160,7 +160,7 @@ void progress_bar::set_pixmap(int n) if (valid_advance_by_number(n)) { sprite = get_tech_sprite(tileset, n); } - NFC_FREE(pix); + delete pix; if (sprite == nullptr) { pix = nullptr; return; @@ -1522,7 +1522,7 @@ void city_dialog::update_prod_buttons() */ city_dialog::~city_dialog() { - NFC_FREE(citizen_pixmap); + delete citizen_pixmap; removeEventFilter(this); } @@ -3148,7 +3148,7 @@ production_item::production_item(struct universal *ptarget, QObject *parent) production_item::~production_item() { // allocated as renegade in model - NFC_FREE(target); + delete target; } /** diff --git a/client/gui-qt/dialogs.cpp b/client/gui-qt/dialogs.cpp index 9d46756074..9c98f1b727 100644 --- a/client/gui-qt/dialogs.cpp +++ b/client/gui-qt/dialogs.cpp @@ -829,7 +829,11 @@ qdef_act *qdef_act::action() /** Deletes qdef_act instance */ -void qdef_act::drop() { NFCN_FREE(m_instance); } +void qdef_act::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Sets default action vs city @@ -1653,7 +1657,8 @@ static void diplomat_queue_handle_primary(int actor_unit_id) fc_assert_action(actor_unit->client.act_prob_cache, client_unit_init_act_prob_cache(actor_unit)); - FC_FREE(actor_unit->client.act_prob_cache); + delete[] actor_unit->client.act_prob_cache; + actor_unit->client.act_prob_cache = nullptr; } // The action selection process is over, at least for now. diff --git a/client/gui-qt/icons.cpp b/client/gui-qt/icons.cpp index 5aa926f280..e516c68a41 100644 --- a/client/gui-qt/icons.cpp +++ b/client/gui-qt/icons.cpp @@ -42,7 +42,11 @@ fcIcons *fcIcons::instance() /** Deletes fc_icons instance */ -void fcIcons::drop() { NFCN_FREE(m_instance); } +void fcIcons::drop() +{ + delete m_instance; + m_instance = nullptr; +} namespace { @@ -198,7 +202,11 @@ hIcon *hIcon::i() return m_instance; } -void hIcon::drop() { NFCN_FREE(m_instance); } +void hIcon::drop() +{ + delete m_instance; + m_instance = nullptr; +} void hIcon::createIcons() { diff --git a/client/gui-qt/idlecallback.cpp b/client/gui-qt/idlecallback.cpp index 1a0dc02201..77bb601c96 100644 --- a/client/gui-qt/idlecallback.cpp +++ b/client/gui-qt/idlecallback.cpp @@ -40,7 +40,11 @@ mrIdle::~mrIdle() /** Deletes current instance */ -void mrIdle::drop() { NFCN_FREE(m_instance); } +void mrIdle::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Returns given instance diff --git a/client/gui-qt/mapview.cpp b/client/gui-qt/mapview.cpp index f221439db6..94b89e751f 100644 --- a/client/gui-qt/mapview.cpp +++ b/client/gui-qt/mapview.cpp @@ -772,7 +772,11 @@ void info_tile::update_font(const QString &name, const QFont &font) /** Deletes current instance */ -void info_tile::drop() { NFCN_FREE(m_instance); } +void info_tile::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Returns given instance diff --git a/client/gui-qt/minimap.cpp b/client/gui-qt/minimap.cpp index ef5ca174ca..5896360fa2 100644 --- a/client/gui-qt/minimap.cpp +++ b/client/gui-qt/minimap.cpp @@ -50,7 +50,11 @@ minimap_view::minimap_view(QWidget *parent) : fcwidget() /** Minimap_view destructor */ -minimap_view::~minimap_view() { NFC_FREE(pix); } +minimap_view::~minimap_view() +{ + delete pix; + pix = nullptr; +} /** Paint event for minimap diff --git a/client/gui-qt/page_game.cpp b/client/gui-qt/page_game.cpp index 7aa8e1455c..2b32b076aa 100644 --- a/client/gui-qt/page_game.cpp +++ b/client/gui-qt/page_game.cpp @@ -246,7 +246,8 @@ void pageGame::updateInfoLabelTimeout() sw_indicators->update(); sw_tax->update(); sw_economy->update(); - FC_FREE(update_info_timer); + delete update_info_timer; + update_info_timer = nullptr; } /** diff --git a/client/gui-qt/page_network.cpp b/client/gui-qt/page_network.cpp index a87f864062..533c070668 100644 --- a/client/gui-qt/page_network.cpp +++ b/client/gui-qt/page_network.cpp @@ -294,7 +294,8 @@ void page_network::destroy_server_scans() if (meta_scan_timer != nullptr) { meta_scan_timer->stop(); meta_scan_timer->disconnect(); - FC_FREE(meta_scan_timer); + delete meta_scan_timer; + meta_scan_timer = nullptr; } if (lan_scan) { @@ -305,7 +306,8 @@ void page_network::destroy_server_scans() if (lan_scan_timer != nullptr) { lan_scan_timer->stop(); lan_scan_timer->disconnect(); - FC_FREE(lan_scan_timer); + delete lan_scan_timer; + lan_scan_timer = nullptr; } #endif } diff --git a/client/gui-qt/sciencedlg.cpp b/client/gui-qt/sciencedlg.cpp index 6c27c95ff4..e0a097da0e 100644 --- a/client/gui-qt/sciencedlg.cpp +++ b/client/gui-qt/sciencedlg.cpp @@ -65,7 +65,7 @@ research_diagram::research_diagram(QWidget *parent) : QWidget(parent) */ research_diagram::~research_diagram() { - NFCN_FREE(tt_help); + delete tt_help; delete pcanvas; destroy_reqtree(req); } @@ -76,7 +76,7 @@ research_diagram::~research_diagram() void research_diagram::update_reqtree() { reset(); - NFCN_FREE(tt_help); + delete tt_help; tt_help = draw_reqtree(req, pcanvas, 0, 0, 0, 0, width, height); update(); } @@ -90,9 +90,8 @@ void research_diagram::reset() if (req != nullptr) { destroy_reqtree(req); } - if (pcanvas != nullptr) { - delete pcanvas; - } + + delete pcanvas; req = create_reqtree(client_player(), true); get_reqtree_dimensions(req, &width, &height); pcanvas = new QPixmap(width, height); @@ -307,8 +306,8 @@ science_report::science_report() : QWidget() */ science_report::~science_report() { - NFC_FREE(curr_list); - NFC_FREE(goal_list); + delete curr_list; + delete goal_list; queen()->removeRepoDlg(QStringLiteral("SCI")); } @@ -358,8 +357,8 @@ void science_report::update_report() fc_assert_ret(nullptr != research); - NFC_FREE(curr_list); - NFC_FREE(goal_list); + delete curr_list; + delete goal_list; if (research->researching != A_UNSET) { total = research->client.researching_cost; diff --git a/client/gui-qt/shortcuts.cpp b/client/gui-qt/shortcuts.cpp index c4a07774ce..de10543233 100644 --- a/client/gui-qt/shortcuts.cpp +++ b/client/gui-qt/shortcuts.cpp @@ -240,7 +240,11 @@ void fc_shortcuts::set_shortcut(fc_shortcut *s) /** Deletes current instance */ -void fc_shortcuts::drop() { NFCN_FREE(m_instance); } +void fc_shortcuts::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Returns given instance diff --git a/client/gui-qt/unitreport.cpp b/client/gui-qt/unitreport.cpp index fc595e55ce..f7f3e38bc6 100644 --- a/client/gui-qt/unitreport.cpp +++ b/client/gui-qt/unitreport.cpp @@ -463,7 +463,11 @@ units_reports *units_reports::instance() /** Deletes units_reports instance */ -void units_reports::drop() { NFCN_FREE(m_instance); } +void units_reports::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Called when close button was pressed diff --git a/client/gui-qt/unitselect.cpp b/client/gui-qt/unitselect.cpp index 99b5a24529..57b0caa0aa 100644 --- a/client/gui-qt/unitselect.cpp +++ b/client/gui-qt/unitselect.cpp @@ -81,7 +81,7 @@ void units_select::create_pixmap() struct unit *punit; float isosize; - NFCN_FREE(pix); + delete pix; isosize = 0.7; if (tileset_hex_height(tileset) > 0 || tileset_hex_width(tileset) > 0) { isosize = 0.5; @@ -97,7 +97,7 @@ void units_select::create_pixmap() item_size.setHeight(tileset_unit_width(tileset) * isosize); } more = false; - NFC_FREE(h_pix); + delete h_pix; h_pix = new QPixmap(item_size.width(), item_size.height()); h_pix->fill(palette().color(QPalette::HighlightedText)); if (unit_count < 5) { @@ -429,6 +429,7 @@ void popdown_unit_sel() units_select *unit_sel = queen()->unit_selector; if (unit_sel != nullptr) { unit_sel->close(); - FC_FREE(unit_sel); + delete unit_sel; + unit_sel = nullptr; } } diff --git a/client/luascript/script_client.cpp b/client/luascript/script_client.cpp index 13e92436b7..95cd65553d 100644 --- a/client/luascript/script_client.cpp +++ b/client/luascript/script_client.cpp @@ -138,7 +138,11 @@ static void script_client_code_init() { script_client_code = nullptr; } /** Free the optional game script code (useful for scenarios). */ -static void script_client_code_free() { NFCN_FREE(script_client_code); } +static void script_client_code_free() +{ + delete[] script_client_code; + script_client_code = nullptr; +} /** Load the optional game script code from file (useful for scenarios). diff --git a/client/mapview_common.cpp b/client/mapview_common.cpp index b69d472930..c1c73dc6ba 100644 --- a/client/mapview_common.cpp +++ b/client/mapview_common.cpp @@ -2408,7 +2408,7 @@ void mapdeco_init() void mapdeco_free() { for (auto *a : qAsConst(*mapdeco_gotoline)) { - NFCPP_FREE(a); + delete a; a = nullptr; } } diff --git a/client/messagewin_common.cpp b/client/messagewin_common.cpp index f4901467fb..a3681bcecd 100644 --- a/client/messagewin_common.cpp +++ b/client/messagewin_common.cpp @@ -72,7 +72,8 @@ void meswin_clear_older(int turn, int phase) delete[] messages[i]->descr; text_tag_list_destroy(messages[i]->tags); - FC_FREE(messages[i]); + delete messages[i]; + messages[i] = nullptr; } if (i != 0) { diff --git a/client/options.cpp b/client/options.cpp index 6732490e5e..101c2e41c3 100644 --- a/client/options.cpp +++ b/client/options.cpp @@ -2744,22 +2744,24 @@ static void server_option_free(struct server_option *poption) { switch (poption->base_option.type) { case OT_STRING: - if (nullptr != poption->string.value) { - FCPP_FREE(poption->string.value); - } - if (nullptr != poption->string.def) { - FCPP_FREE(poption->string.def); - } + delete[] poption->string.value; + delete[] poption->string.def; + poption->string.value = nullptr; + poption->string.def = nullptr; break; case OT_ENUM: - NFCN_FREE(poption->enumerator.support_names); - NFCN_FREE(poption->enumerator.pretty_names); + delete poption->enumerator.support_names; + delete poption->enumerator.pretty_names; + poption->enumerator.support_names = nullptr; + poption->enumerator.pretty_names = nullptr; break; case OT_BITWISE: - NFCN_FREE(poption->bitwise.support_names); - NFCN_FREE(poption->bitwise.pretty_names); + delete poption->bitwise.support_names; + delete poption->bitwise.pretty_names; + poption->bitwise.support_names = nullptr; + poption->bitwise.pretty_names = nullptr; break; case OT_BOOLEAN: @@ -2769,9 +2771,12 @@ static void server_option_free(struct server_option *poption) break; } - NFCNPP_FREE(poption->name); - NFCNPP_FREE(poption->description); - NFCNPP_FREE(poption->help_text); + delete[] poption->name; + delete[] poption->description; + delete[] poption->help_text; + poption->name = nullptr; + poption->description = nullptr; + poption->help_text = nullptr; } /** @@ -2789,7 +2794,8 @@ void server_options_free() for (i = 0; i < server_options_num; i++) { server_option_free(server_options + i); } - FCPP_FREE(server_options); + delete[] server_options; + server_options = nullptr; server_options_num = 0; } @@ -2797,10 +2803,12 @@ void server_options_free() if (nullptr != server_options_categories) { for (i = 0; i < server_options_categories_num; i++) { if (nullptr != server_options_categories[i]) { - FCPP_FREE(server_options_categories[i]); + delete[] server_options_categories[i]; + server_options_categories[i] = nullptr; } } - FCPP_FREE(server_options_categories); + delete[] server_options_categories; + server_options_categories = nullptr; server_options_categories_num = 0; } } @@ -4768,17 +4776,21 @@ void options_free() case OT_ENUM: fc_assert_action(nullptr != pcoption->u.enumerator.support_names, break); - FC_FREE(pcoption->u.enumerator.support_names); + delete pcoption->u.enumerator.support_names; + pcoption->u.enumerator.support_names = nullptr; fc_assert_action(nullptr != pcoption->u.enumerator.pretty_names, break); - FC_FREE(pcoption->u.enumerator.pretty_names); + delete pcoption->u.enumerator.pretty_names; + pcoption->u.enumerator.pretty_names = nullptr; break; case OT_BITWISE: fc_assert_action(nullptr != pcoption->u.bitwise.support_names, break); - FC_FREE(pcoption->u.bitwise.support_names); + delete pcoption->u.bitwise.support_names; + pcoption->u.bitwise.support_names = nullptr; fc_assert_action(nullptr != pcoption->u.bitwise.pretty_names, break); - FC_FREE(pcoption->u.bitwise.pretty_names); + delete pcoption->u.bitwise.pretty_names; + pcoption->u.bitwise.pretty_names = nullptr; break; case OT_BOOLEAN: diff --git a/client/packhand.cpp b/client/packhand.cpp index 5c6bc89281..3e96b82866 100644 --- a/client/packhand.cpp +++ b/client/packhand.cpp @@ -668,7 +668,8 @@ void handle_city_info(const struct packet_city_info *packet) struct trade_route *proute = trade_route_list_get(pcity->routes, -1); trade_route_list_remove(pcity->routes, proute); - FC_FREE(proute); + delete proute; + proute = nullptr; trade_routes_changed = true; } @@ -1241,7 +1242,8 @@ void handle_worker_task(const struct packet_worker_task *packet) } else { if (packet->activity == ACTIVITY_LAST) { worker_task_list_remove(pcity->task_reqs, ptask); - FC_FREE(ptask); + delete ptask; + ptask = nullptr; } } @@ -3077,13 +3079,13 @@ void handle_tile_info(const struct packet_tile_info *packet) if (packet->spec_sprite[0] != '\0') { if (!ptile->spec_sprite || strcmp(ptile->spec_sprite, packet->spec_sprite) != 0) { - NFC_FREE(ptile->spec_sprite); + delete[] ptile->spec_sprite; ptile->spec_sprite = fc_strdup(packet->spec_sprite); tile_changed = true; } } else { if (ptile->spec_sprite) { - delete ptile->spec_sprite; + delete[] ptile->spec_sprite; ptile->spec_sprite = nullptr; tile_changed = true; } @@ -3109,7 +3111,7 @@ void handle_tile_info(const struct packet_tile_info *packet) if (packet->label[0] == '\0') { if (ptile->label != nullptr) { - FC_FREE(ptile->label); + delete[] ptile->label; ptile->label = nullptr; tile_changed = true; } @@ -3278,7 +3280,7 @@ void handle_ruleset_summary(const struct packet_ruleset_summary *packet) { int len; - NFCPP_FREE(game.ruleset_summary); + delete[] game.ruleset_summary; len = qstrlen(packet->text); game.ruleset_summary = new char[len + 1]; @@ -3809,7 +3811,7 @@ void handle_ruleset_terrain(const struct packet_ruleset_terrain *p) output_type_iterate(o) { pterrain->output[o] = p->output[o]; } output_type_iterate_end; - NFCPP_FREE(pterrain->resources); + delete[] pterrain->resources; pterrain->resources = new extra_type *[p->num_resources + 1](); for (j = 0; j < p->num_resources; j++) { pterrain->resources[j] = extra_by_number(p->resources[j]); diff --git a/client/reqtree.cpp b/client/reqtree.cpp index 0d42114a66..f67671bff5 100644 --- a/client/reqtree.cpp +++ b/client/reqtree.cpp @@ -438,8 +438,8 @@ void destroy_reqtree(struct reqtree *tree) int i; for (i = 0; i < tree->num_nodes; i++) { - delete[] tree->nodes[i]->require; - delete[] tree->nodes[i]->provide; + free(tree->nodes[i]->require); + free(tree->nodes[i]->provide); delete[] tree->nodes[i]; } delete[] tree->nodes; diff --git a/client/servers.cpp b/client/servers.cpp index fde62fcf05..a6a7888aae 100644 --- a/client/servers.cpp +++ b/client/servers.cpp @@ -104,7 +104,11 @@ void fcUdpScan::sockError(QAbstractSocket::SocketError socketError) /************************************************************************** deletes fcUdpScan **************************************************************************/ -void fcUdpScan::drop() { NFCN_FREE(m_instance); } +void fcUdpScan::drop() +{ + delete m_instance; + m_instance = nullptr; +} /** Broadcast an UDP package to all servers on LAN, requesting information diff --git a/client/tilespec.cpp b/client/tilespec.cpp index 2734d0e84a..f1c4565f93 100644 --- a/client/tilespec.cpp +++ b/client/tilespec.cpp @@ -922,9 +922,7 @@ static void tileset_free_toplevel(struct tileset *t) { int i; - if (t->main_intro_filename) { - FCPP_FREE(t->main_intro_filename); - } + delete[] t->main_intro_filename; if (t->preferred_themes) { for (i = 0; i < t->num_preferred_themes; i++) { @@ -951,9 +949,12 @@ static void tileset_free_toplevel(struct tileset *t) t->color_system = nullptr; } - NFCNPP_FREE(t->summary); - NFCNPP_FREE(t->description); - NFCNPP_FREE(t->for_ruleset); + delete[] t->summary; + delete[] t->description; + delete[] t->for_ruleset; + t->summary = nullptr; + t->description = nullptr; + t->for_ruleset = nullptr; } /** @@ -1576,7 +1577,7 @@ static void tileset_stop_read(struct tileset *t, struct section_file *file, { secfile_destroy(file); delete[] fname; - NFCPP_FREE(layer_order); + delete[] layer_order; delete[] t; if (nullptr != sections) { section_list_destroy(sections); @@ -1717,7 +1718,8 @@ static struct tileset *tileset_read_toplevel(const char *tileset_name, fc_strlcpy(t->summary, tstr, len + 1); } else { // No summary - NFCNPP_FREE(t->summary); + delete[] t->summary; + t->summary = nullptr; } tstr = secfile_lookup_str_default(file, "", "tilespec.description"); @@ -1731,7 +1733,8 @@ static struct tileset *tileset_read_toplevel(const char *tileset_name, } else { // No description if (t->description != nullptr) { - FCPP_FREE(t->description); + delete[] t->description; + t->description = nullptr; } } @@ -2200,7 +2203,7 @@ static struct tileset *tileset_read_toplevel(const char *tileset_name, secfile_destroy(file); qDebug("finished reading \"%s\".", fname); delete[] fname; - NFCPP_FREE(layer_order); + delete[] layer_order; return t; } diff --git a/client/update_queue.cpp b/client/update_queue.cpp index e7a1cd211d..bba5df908e 100644 --- a/client/update_queue.cpp +++ b/client/update_queue.cpp @@ -59,7 +59,11 @@ update_queue *update_queue::uq() return m_instance; } -void update_queue::drop() { NFCN_FREE(m_instance); } +void update_queue::drop() +{ + delete m_instance; + m_instance = nullptr; +} // Extract the update_queue_data from the waiting queue data. struct update_queue_data * diff --git a/client/voteinfo.cpp b/client/voteinfo.cpp index 686bac3a66..c325de6ae1 100644 --- a/client/voteinfo.cpp +++ b/client/voteinfo.cpp @@ -183,7 +183,11 @@ void voteinfo_queue_free() return; } - voteinfo_list_iterate(voteinfo_queue, vi) { NFC_FREE(vi); } + voteinfo_list_iterate(voteinfo_queue, vi) + { + delete vi; + vi = nullptr; + } voteinfo_list_iterate_end; voteinfo_list_destroy(voteinfo_queue); diff --git a/common/achievements.cpp b/common/achievements.cpp index 0b83e9f930..77f19c8377 100644 --- a/common/achievements.cpp +++ b/common/achievements.cpp @@ -63,8 +63,8 @@ void achievements_free() int i; for (i = 0; i < ARRAY_SIZE(achievements); i++) { - NFCPP_FREE(achievements[i].first_msg); - NFCPP_FREE(achievements[i].cons_msg); + delete[] achievements[i].first_msg; + delete[] achievements[i].cons_msg; } } diff --git a/common/actions.cpp b/common/actions.cpp index b059e78335..6d64b63f61 100644 --- a/common/actions.cpp +++ b/common/actions.cpp @@ -1014,7 +1014,8 @@ void actions_free() action_enabler_list_destroy(action_enablers_by_action[act]); - FC_FREE(actions[act]); + delete actions[act]; + actions[act] = nullptr; } action_iterate_end; diff --git a/common/aicore/cm.cpp b/common/aicore/cm.cpp index 732e675aff..d719d855a1 100644 --- a/common/aicore/cm.cpp +++ b/common/aicore/cm.cpp @@ -2025,9 +2025,12 @@ static void cm_state_free(struct cm_state *state) destroy_partial_solution(&state->best); destroy_partial_solution(&state->current); - FCPP_FREE(state->choice.stack); - FCPP_FREE(state->workers_map); - FC_FREE(state); + delete[] state->choice.stack; + delete[] state->workers_map; + state->choice.stack = nullptr; + state->workers_map = nullptr; + delete state; + state = nullptr; } /** diff --git a/common/aicore/path_finding.cpp b/common/aicore/path_finding.cpp index a8e9b48af0..4b8f048bac 100644 --- a/common/aicore/path_finding.cpp +++ b/common/aicore/path_finding.cpp @@ -1550,7 +1550,8 @@ static bool pf_danger_map_iterate(struct pf_map *pfm) node1->extra_cost = extra; node1->cost = cost; node1->dir_to_here = dir; - NFCN_FREE(node1->danger_segment); + delete[] node1->danger_segment; + node1->danger_segment = nullptr; if (node->is_dangerous) { /* We came from a dangerous tile. So we need to record the * path we came from until the previous safe position is @@ -1777,9 +1778,10 @@ static void pf_danger_map_destroy(struct pf_map *pfm) // Need to clean up the dangling danger segments. for (i = 0, node = pfdm->lattice; i < MAP_INDEX_SIZE; i++, node++) { - NFC_FREE(node->danger_segment); + delete[] node->danger_segment; + node->danger_segment = nullptr; } - delete pfdm->lattice; + delete[] pfdm->lattice; map_index_pq_destroy(pfdm->queue); map_index_pq_destroy(pfdm->danger_queue); delete pfdm; diff --git a/common/citizens.cpp b/common/citizens.cpp index 576f91ca76..06cd63874f 100644 --- a/common/citizens.cpp +++ b/common/citizens.cpp @@ -56,9 +56,8 @@ void citizens_free(struct city *pcity) { fc_assert_ret(pcity); - if (pcity->nationality) { - FCPP_FREE(pcity->nationality); - } + delete[] pcity->nationality; + pcity->nationality = nullptr; } /** diff --git a/common/city.cpp b/common/city.cpp index 0633dce650..35425afd4f 100644 --- a/common/city.cpp +++ b/common/city.cpp @@ -3330,7 +3330,8 @@ void city_styles_free() requirement_vector_free(&city_styles[i].reqs); } - FCPP_FREE(city_styles); + delete[] city_styles; + city_styles = nullptr; game.control.styles_count = 0; } @@ -3432,7 +3433,8 @@ void destroy_city_virtual(struct city *pcity) if (pcity->tile_cache != nullptr) { free(pcity->tile_cache); // realloc } - NFC_FREE(pcity->cm_parameter); + delete pcity->cm_parameter; + pcity->cm_parameter = nullptr; if (!is_server()) { unit_list_destroy(pcity->client.info_units_supported); unit_list_destroy(pcity->client.info_units_present); @@ -3447,7 +3449,8 @@ void destroy_city_virtual(struct city *pcity) } memset(pcity, 0, sizeof(*pcity)); // ensure no pointers remain - FCPP_FREE(pcity); + delete[] pcity; + pcity = nullptr; } /** diff --git a/common/clientutils.cpp b/common/clientutils.cpp index 7d57bff68a..1c8a97234b 100644 --- a/common/clientutils.cpp +++ b/common/clientutils.cpp @@ -179,7 +179,8 @@ static void calc_activity(struct actcalc *calc, const struct tile *ptile, } } activity_type_iterate_end; - FCPP_FREE(t); + delete[] t; + t = nullptr; } /** @@ -212,7 +213,8 @@ int turns_to_activity_done(const struct tile *ptile, Activity_type_id act, turns = calc->activity_turns[act]; } - FC_FREE(calc); + delete calc; + calc = nullptr; return turns; } @@ -294,7 +296,7 @@ QString concat_tile_activity_text(struct tile *ptile) } activity_type_iterate_end; - FC_FREE(calc); - + delete calc; + calc = nullptr; return qUtf8Printable(str); } diff --git a/common/extras.cpp b/common/extras.cpp index 48fc405f13..d96c8b48e4 100644 --- a/common/extras.cpp +++ b/common/extras.cpp @@ -81,9 +81,12 @@ void extras_free() resource_types_free(); for (i = 0; i < game.control.num_extra_types; i++) { - NFCN_FREE(extras[i].data.base); - NFCN_FREE(extras[i].data.road); - NFCN_FREE(extras[i].data.resource); + delete extras[i].data.base; + delete extras[i].data.road; + delete extras[i].data.resource; + extras[i].data.base = nullptr; + extras[i].data.road = nullptr; + extras[i].data.resource = nullptr; } for (i = 0; i < EC_LAST; i++) { @@ -104,7 +107,8 @@ void extras_free() requirement_vector_free(&(extras[i].rmreqs)); requirement_vector_free(&(extras[i].appearance_reqs)); requirement_vector_free(&(extras[i].disappearance_reqs)); - NFCN_FREE(extras[i].helptext); + delete extras[i].helptext; + extras[i].helptext = nullptr; } extra_type_iterate(pextra) @@ -866,11 +870,13 @@ void set_user_extra_flag_name(enum extra_flag_id id, const char *name, int efid = id - EF_USER_FLAG_1; fc_assert_ret(id >= EF_USER_FLAG_1 && id <= EF_LAST_USER_FLAG); - NFCN_FREE(user_extra_flags[efid].name); + delete[] user_extra_flags[efid].name; + user_extra_flags[efid].name = nullptr; if (name && name[0] != '\0') { user_extra_flags[efid].name = fc_strdup(name); } - NFCNPP_FREE(user_extra_flags[efid].helptxt); + delete[] user_extra_flags[efid].helptxt; + user_extra_flags[efid].helptxt = nullptr; if (helptxt && helptxt[0] != '\0') { user_extra_flags[efid].helptxt = fc_strdup(helptxt); } diff --git a/common/game.cpp b/common/game.cpp index ba56a433a5..6c2bc775e2 100644 --- a/common/game.cpp +++ b/common/game.cpp @@ -592,8 +592,10 @@ void game_ruleset_free() if (game.server.luadata != nullptr) { secfile_destroy(game.server.luadata); } - NFCNPP_FREE(game.server.ruledit.description_file); - NFCNPP_FREE(game.server.ruledit.nationlist); + delete[] game.server.ruledit.description_file; + game.server.ruledit.description_file = nullptr; + delete[] game.server.ruledit.nationlist; + game.server.ruledit.nationlist = nullptr; if (game.server.ruledit.embedded_nations != nullptr) { for (i = 0; i < game.server.ruledit.embedded_nations_count; i++) { delete[] game.server.ruledit.embedded_nations[i]; @@ -632,9 +634,12 @@ void game_ruleset_free() game.calendar.calendar_fragment_name[i][0] = '\0'; } - NFCNPP_FREE(game.ruleset_summary); - NFCNPP_FREE(game.ruleset_description); - NFCNPP_FREE(game.ruleset_capabilities); + delete[] game.ruleset_summary; + delete[] game.ruleset_description; + delete[] game.ruleset_capabilities; + game.ruleset_summary = nullptr; + game.ruleset_description = nullptr; + game.ruleset_capabilities = nullptr; } /** @@ -797,8 +802,10 @@ void user_flag_init(struct user_flag *flag) */ void user_flag_free(struct user_flag *flag) { - NFCNPP_FREE(flag->name); - NFCNPP_FREE(flag->helptxt); + delete[] flag->name; + delete[] flag->helptxt; + flag->name = nullptr; + flag->helptxt = nullptr; } /** diff --git a/common/government.cpp b/common/government.cpp index 211420c789..ff9ab02c37 100644 --- a/common/government.cpp +++ b/common/government.cpp @@ -486,8 +486,10 @@ static inline void government_free(struct government *pgovern) for (auto *a : pgovern->ruler_titles->values()) { delete a; } - FC_FREE(pgovern->ruler_titles); - NFCN_FREE(pgovern->helptext); + delete pgovern->ruler_titles; + delete pgovern->helptext; + pgovern->ruler_titles = nullptr; + pgovern->helptext = nullptr; requirement_vector_free(&pgovern->reqs); } @@ -522,7 +524,8 @@ void governments_free() for (i = 0; i < game.control.government_count; i++) { government_free(governments + i); } - FCPP_FREE(governments); + delete[] governments; + governments = nullptr; game.control.government_count = 0; } diff --git a/common/helpdata.cpp b/common/helpdata.cpp index 24c74e4740..af6bcdff28 100644 --- a/common/helpdata.cpp +++ b/common/helpdata.cpp @@ -78,11 +78,12 @@ void free_help_texts() return; } for (const auto *ptmp : qAsConst(*help_nodes)) { - NFCPP_FREE(ptmp->topic); - NFCPP_FREE(ptmp->text); - NFC_FREE(ptmp); + delete[] ptmp->topic; + delete[] ptmp->text; + delete ptmp; } - FC_FREE(help_nodes); + delete help_nodes; + help_nodes = nullptr; } /** @@ -840,7 +841,8 @@ void boot_help_texts(const nation_set *nations_to_show, category_nodes.append(pitem); } extra_type_iterate_end; - FCPP_FREE(cats); + delete[] cats; + cats = nullptr; } break; case HELP_GOODS: goods_type_iterate(pgood) @@ -1086,7 +1088,8 @@ void boot_help_texts(const nation_set *nations_to_show, sz_strlcat(long_buffer, "\n\n"); } } - FCPP_FREE(paras); + delete[] paras; + paras = nullptr; pitem->text = qstrdup(long_buffer); help_nodes->append(pitem); } diff --git a/common/idex.cpp b/common/idex.cpp index a70120bf77..caf8045260 100644 --- a/common/idex.cpp +++ b/common/idex.cpp @@ -52,8 +52,10 @@ void idex_init(struct world *iworld) */ void idex_free(struct world *iworld) { - FC_FREE(iworld->cities); - FC_FREE(iworld->units); + delete iworld->cities; + delete iworld->units; + iworld->cities = nullptr; + iworld->units = nullptr; } /** diff --git a/common/improvement.cpp b/common/improvement.cpp index c0748a4d52..2badb7bfc2 100644 --- a/common/improvement.cpp +++ b/common/improvement.cpp @@ -62,7 +62,8 @@ void improvements_init() */ static void improvement_free(struct impr_type *p) { - NFCN_FREE(p->helptext); + delete p->helptext; + p->helptext = nullptr; requirement_vector_free(&p->reqs); requirement_vector_free(&p->obsolete_by); } diff --git a/common/map.cpp b/common/map.cpp index cc452856bf..f91610c155 100644 --- a/common/map.cpp +++ b/common/map.cpp @@ -455,15 +455,11 @@ static void tile_free(struct tile *ptile) { unit_list_destroy(ptile->units); - if (ptile->spec_sprite) { - free(ptile->spec_sprite); - ptile->spec_sprite = nullptr; - } + delete[] ptile->spec_sprite; + ptile->spec_sprite = nullptr; - if (ptile->label) { - FCPP_FREE(ptile->label); - ptile->label = nullptr; - } + delete[] ptile->label; + ptile->label = nullptr; } /** @@ -488,7 +484,7 @@ void map_allocate(struct civ_map *amap) tile_init(ptile); } whole_map_iterate_end; - NFC_FREE(amap->startpos_table); + delete amap->startpos_table; amap->startpos_table = new QHash; } @@ -514,16 +510,19 @@ void map_free(struct civ_map *fmap) whole_map_iterate(fmap, ptile) { tile_free(ptile); } whole_map_iterate_end; - FCPP_FREE(fmap->tiles); + delete[] fmap->tiles; + fmap->tiles = nullptr; if (fmap->startpos_table) { for (auto *a : qAsConst(*fmap->startpos_table)) { startpos_destroy(a); } - FC_FREE(fmap->startpos_table); + delete fmap->startpos_table; + fmap->startpos_table = nullptr; } - FCPP_FREE(fmap->iterate_outwards_indices); + delete[] fmap->iterate_outwards_indices; + fmap->iterate_outwards_indices = nullptr; } } diff --git a/common/mapimg.cpp b/common/mapimg.cpp index e8d7cc1219..b3e1f1c2dd 100644 --- a/common/mapimg.cpp +++ b/common/mapimg.cpp @@ -1924,8 +1924,9 @@ static void img_destroy(struct img *pimg) { if (pimg != nullptr) { // do not free pimg->def - free(pimg->map); - free(pimg); + delete[] pimg->map; + delete pimg; + pimg = nullptr; } } diff --git a/common/multipliers.cpp b/common/multipliers.cpp index ce46df104b..f22a5a35b4 100644 --- a/common/multipliers.cpp +++ b/common/multipliers.cpp @@ -46,7 +46,8 @@ void multipliers_free() multipliers_iterate(pmul) { requirement_vector_free(&(pmul->reqs)); - NFCN_FREE(pmul->helptext); + delete pmul->helptext; + pmul->helptext = nullptr; } multipliers_iterate_end; } diff --git a/common/networking/connection.cpp b/common/networking/connection.cpp index efe85067b1..eceaddfcf9 100644 --- a/common/networking/connection.cpp +++ b/common/networking/connection.cpp @@ -408,6 +408,7 @@ static void free_socket_packet_buffer(struct socket_packet_buffer *buf) free(buf->data); } delete buf; + buf = nullptr; } } @@ -518,14 +519,9 @@ void free_compression_queue(struct connection *pc) */ static void init_packet_hashs(struct connection *pc) { - pc->phs.sent = new genhash *[PACKET_LAST]; - pc->phs.received = new genhash *[PACKET_LAST]; + pc->phs.sent = new genhash *[PACKET_LAST] {}; + pc->phs.received = new genhash *[PACKET_LAST] {}; pc->phs.handlers = packet_handlers_initial(); - - for (int i = 0; i < PACKET_LAST; i++) { - pc->phs.sent[i] = nullptr; - pc->phs.received[i] = nullptr; - } } /** @@ -541,7 +537,8 @@ static void free_packet_hashes(struct connection *pc) genhash_destroy(pc->phs.sent[i]); } } - FCPP_FREE(pc->phs.sent); + delete[] pc->phs.sent; + pc->phs.sent = nullptr; } if (pc->phs.received) { @@ -550,7 +547,8 @@ static void free_packet_hashes(struct connection *pc) genhash_destroy(pc->phs.received[i]); } } - FCPP_FREE(pc->phs.received); + delete[] pc->phs.received; + pc->phs.received = nullptr; } } diff --git a/common/networking/packets.cpp b/common/networking/packets.cpp index 94478539e8..a28d7815a0 100644 --- a/common/networking/packets.cpp +++ b/common/networking/packets.cpp @@ -668,7 +668,7 @@ void generic_handle_player_attribute_chunk( != pplayer->attribute_block_buffer.length)) { // wrong attribute data if (pplayer->attribute_block_buffer.data) { - free(pplayer->attribute_block_buffer.data); + delete[] pplayer->attribute_block_buffer.data; pplayer->attribute_block_buffer.data = nullptr; } pplayer->attribute_block_buffer.length = 0; @@ -678,20 +678,19 @@ void generic_handle_player_attribute_chunk( // first one in a row if (chunk->offset == 0) { if (pplayer->attribute_block_buffer.data) { - free(pplayer->attribute_block_buffer.data); + delete[] pplayer->attribute_block_buffer.data; pplayer->attribute_block_buffer.data = nullptr; } - pplayer->attribute_block_buffer.data = fc_malloc(chunk->total_length); + pplayer->attribute_block_buffer.data = new char[chunk->total_length]{}; pplayer->attribute_block_buffer.length = chunk->total_length; } - memcpy(static_cast(pplayer->attribute_block_buffer.data) - + chunk->offset, - chunk->data, chunk->chunk_length); + memcpy(pplayer->attribute_block_buffer.data + chunk->offset, chunk->data, + chunk->chunk_length); if (chunk->offset + chunk->chunk_length == chunk->total_length) { // Received full attribute block if (pplayer->attribute_block.data != nullptr) { - free(pplayer->attribute_block.data); + delete[] pplayer->attribute_block.data; } pplayer->attribute_block.data = pplayer->attribute_block_buffer.data; pplayer->attribute_block.length = pplayer->attribute_block_buffer.length; @@ -729,9 +728,7 @@ void send_attribute_block(const struct player *pplayer, packet.total_length = pplayer->attribute_block.length; packet.chunk_length = size_of_current_chunk; - memcpy(packet.data, - static_cast(pplayer->attribute_block.data) - + packet.offset, + memcpy(packet.data, pplayer->attribute_block.data + packet.offset, packet.chunk_length); bytes_left -= packet.chunk_length; diff --git a/common/player.cpp b/common/player.cpp index 841a872ac4..4ee400572f 100644 --- a/common/player.cpp +++ b/common/player.cpp @@ -637,13 +637,14 @@ void player_clear(struct player *pplayer, bool full) return; } - NFCNPP_FREE(pplayer->savegame_ai_type_name); + delete[] pplayer->savegame_ai_type_name; + pplayer->savegame_ai_type_name = nullptr; // Clears the attribute blocks. - VOIDNFCN_FREE(pplayer->attribute_block.data); + delete[] pplayer->attribute_block.data; pplayer->attribute_block.length = 0; - VOIDNFCN_FREE(pplayer->attribute_block_buffer.data); + delete[] pplayer->attribute_block_buffer.data; pplayer->attribute_block_buffer.length = 0; // Clears units and cities. @@ -747,6 +748,7 @@ void player_destroy(struct player *pplayer) } delete[] pplayer; + pplayer = nullptr; pslot->player = nullptr; player_slots.used_slots--; } @@ -1706,9 +1708,8 @@ static bv_diplrel_all_reqs *diplrel_mess_get() */ void diplrel_mess_close() { - if (diplrel_mess != nullptr) { - FCPP_FREE(diplrel_mess); - } + delete[] diplrel_mess; + diplrel_mess = nullptr; } /** diff --git a/common/player.h b/common/player.h index aff423743e..a131089549 100644 --- a/common/player.h +++ b/common/player.h @@ -208,7 +208,7 @@ enum player_debug_types { BV_DEFINE(bv_debug, PLAYER_DEBUG_LAST); struct attribute_block_s { - void *data; + char *data; int length; #define MAX_ATTRIBUTE_BLOCK (256 * 1024) // largest attribute block }; diff --git a/common/scriptcore/luascript.cpp b/common/scriptcore/luascript.cpp index 3946dadc59..97189bdc55 100644 --- a/common/scriptcore/luascript.cpp +++ b/common/scriptcore/luascript.cpp @@ -281,7 +281,8 @@ struct fc_lua *luascript_new(luascript_log_func_t output_fct, fcl->state = luaL_newstate(); if (!fcl->state) { - FCPP_FREE(fcl); + delete[] fcl; + fcl = nullptr; return nullptr; } fcl->output_fct = output_fct; diff --git a/common/scriptcore/luascript_signal.cpp b/common/scriptcore/luascript_signal.cpp index b7911cc318..aedfaefb6d 100644 --- a/common/scriptcore/luascript_signal.cpp +++ b/common/scriptcore/luascript_signal.cpp @@ -327,9 +327,9 @@ void luascript_signal_free(struct fc_lua *fcl) for (auto *nissan : qAsConst(*fcl->signals_hash)) { signal_destroy(nissan); } - NFC_FREE(fcl->signals_hash); + delete fcl->signals_hash; + delete fcl->signal_names; fcl->signals_hash = nullptr; - NFC_FREE(fcl->signal_names); fcl->signal_names = nullptr; } diff --git a/common/specialist.cpp b/common/specialist.cpp index 44ffc2a40e..db46f8c3a6 100644 --- a/common/specialist.cpp +++ b/common/specialist.cpp @@ -56,7 +56,8 @@ void specialists_free() struct specialist *p = &specialists[i]; requirement_vector_free(&p->reqs); - NFCN_FREE(p->helptext); + delete p->helptext; + p->helptext = nullptr; } } diff --git a/common/style.cpp b/common/style.cpp index 304f429b05..d6e892aa4d 100644 --- a/common/style.cpp +++ b/common/style.cpp @@ -46,7 +46,11 @@ void styles_alloc(int count) /** Free the memory associated with styles */ -void styles_free() { FCPP_FREE(styles); } +void styles_free() +{ + delete[] styles; + styles = nullptr; +} /** Return the number of styles. diff --git a/common/team.cpp b/common/team.cpp index 13b7cdc803..e4bcadcafc 100644 --- a/common/team.cpp +++ b/common/team.cpp @@ -85,11 +85,11 @@ void team_slots_free() if (nullptr != tslot->team) { team_destroy(tslot->team); } - NFCPP_FREE(tslot->defined_name); - NFCPP_FREE(tslot->rule_name); + delete[] tslot->defined_name; + delete[] tslot->rule_name; #ifdef FREECIV_ENABLE_NLS - NFCPP_FREE(tslot->name_translation); + delete[] tslot->name_translation; #endif // FREECIV_ENABLE_NLS } team_slots_iterate_end; @@ -280,14 +280,13 @@ void team_slot_set_defined_name(struct team_slot *tslot, fc_assert_ret(nullptr != tslot); fc_assert_ret(nullptr != team_name); - NFCPP_FREE(tslot->defined_name); + delete[] tslot->defined_name; tslot->defined_name = fc_strdup(team_name); - NFCPP_FREE(tslot->rule_name); - + delete[] tslot->rule_name; tslot->rule_name = fc_strdup(Qn_(team_name)); #ifdef FREECIV_ENABLE_NLS - NFCPP_FREE(tslot->name_translation); + delete[] tslot->name_translation; tslot->name_translation = fc_strdup(Q_(team_name)); #endif // FREECIV_ENABLE_NLS } @@ -347,7 +346,8 @@ void team_destroy(struct team *pteam) fc_assert(tslot->team == pteam); player_list_destroy(pteam->plrlist); - FCPP_FREE(pteam); + delete[] pteam; + pteam = nullptr; tslot->team = nullptr; team_slots.used_slots--; } diff --git a/common/tech.cpp b/common/tech.cpp index 94e3374041..1b82806ff3 100644 --- a/common/tech.cpp +++ b/common/tech.cpp @@ -389,14 +389,14 @@ void set_user_tech_flag_name(enum tech_flag_id id, const char *name, fc_assert_ret(id >= TECH_USER_1 && id <= TECH_USER_LAST); - NFCN_FREE(user_tech_flags[tfid].name); - + delete[] user_tech_flags[tfid].name; + user_tech_flags[tfid].name = nullptr; if (name && name[0] != '\0') { user_tech_flags[tfid].name = fc_strdup(name); } - NFCN_FREE(user_tech_flags[tfid].helptxt); - + delete[] user_tech_flags[tfid].helptxt; + user_tech_flags[tfid].helptxt = nullptr; if (helptxt && helptxt[0] != '\0') { user_tech_flags[tfid].helptxt = fc_strdup(helptxt); } @@ -476,8 +476,10 @@ void techs_init() static void tech_free(Tech_type_id tech) { struct advance *p = &advances[tech]; - NFCN_FREE(p->helptext); - NFCNPP_FREE(p->bonus_message); + delete p->helptext; + p->helptext = nullptr; + delete[] p->bonus_message; + p->bonus_message = nullptr; } /** diff --git a/common/terrain.cpp b/common/terrain.cpp index 0f2437e2f7..5a8ae68bfd 100644 --- a/common/terrain.cpp +++ b/common/terrain.cpp @@ -55,8 +55,10 @@ void terrains_free() { terrain_type_iterate(pterrain) { - NFCN_FREE(pterrain->helptext); - NFCNPP_FREE(pterrain->resources); + delete pterrain->helptext; + pterrain->helptext = nullptr; + delete[] pterrain->resources; + pterrain->resources = nullptr; if (pterrain->rgb != nullptr) { /* Server allocates this on ruleset loading, client when * ruleset packet is received. */ @@ -795,14 +797,14 @@ void set_user_terrain_flag_name(enum terrain_flag_id id, const char *name, fc_assert_ret(id >= TER_USER_1 && id <= TER_USER_LAST); - NFCN_FREE(user_terrain_flags[tfid].name); - + delete[] user_terrain_flags[tfid].name; + user_terrain_flags[tfid].name = nullptr; if (name && name[0] != '\0') { user_terrain_flags[tfid].name = fc_strdup(name); } - NFCN_FREE(user_terrain_flags[tfid].helptxt); - + delete[] user_terrain_flags[tfid].helptxt; + user_terrain_flags[tfid].helptxt = nullptr; if (helptxt && helptxt[0] != '\0') { user_terrain_flags[tfid].helptxt = fc_strdup(helptxt); } diff --git a/common/tile.cpp b/common/tile.cpp index 0ab18c271b..774d0842e0 100644 --- a/common/tile.cpp +++ b/common/tile.cpp @@ -1153,7 +1153,7 @@ bool tile_set_label(struct tile *ptile, const char *label) } else if (strcmp(ptile->label, label)) { changed = true; } - FC_FREE(ptile->label); + delete[] ptile->label; ptile->label = nullptr; } else if (label != nullptr) { changed = true; diff --git a/common/traderoutes.cpp b/common/traderoutes.cpp index 2bca99787d..7580f7a407 100644 --- a/common/traderoutes.cpp +++ b/common/traderoutes.cpp @@ -561,7 +561,8 @@ void goods_free() for (i = 0; i < MAX_GOODS_TYPES; i++) { requirement_vector_free(&(goods[i].reqs)); - NFCN_FREE(goods[i].helptext); + delete[] goods[i].helptext; + goods[i].helptext = nullptr; } } diff --git a/common/unit.cpp b/common/unit.cpp index 559160d771..39c683fc85 100644 --- a/common/unit.cpp +++ b/common/unit.cpp @@ -1627,15 +1627,18 @@ void unit_virtual_destroy(struct unit *punit) CALL_FUNC_EACH_AI(unit_free, punit); if (is_server() && punit->server.adv) { - FCPP_FREE(punit->server.adv); + delete[] punit->server.adv; + punit->server.adv = nullptr; } else { if (punit->client.act_prob_cache) { - FCPP_FREE(punit->client.act_prob_cache); + delete[] punit->client.act_prob_cache; + punit->client.act_prob_cache = nullptr; } } if (--punit->refcount <= 0) { - FC_FREE(punit); + delete punit; + punit = nullptr; } } @@ -1647,7 +1650,8 @@ void free_unit_orders(struct unit *punit) { if (punit->has_orders) { punit->goto_tile = nullptr; - FCPP_FREE(punit->orders.list); + delete[] punit->orders.list; + punit->orders.list = nullptr; } punit->orders.length = 0; punit->has_orders = false; diff --git a/common/unittype.cpp b/common/unittype.cpp index df613863e8..f2f1dd4f65 100644 --- a/common/unittype.cpp +++ b/common/unittype.cpp @@ -1556,7 +1556,7 @@ void set_user_unit_class_flag_name(enum unit_class_flag_id id, fc_assert_ret(id >= UCF_USER_FLAG_1 && id <= UCF_LAST_USER_FLAG); if (user_class_flags[ufid].name != nullptr) { - FC_FREE(user_class_flags[ufid].name); + delete[] user_class_flags[ufid].name; user_class_flags[ufid].name = nullptr; } @@ -1565,7 +1565,7 @@ void set_user_unit_class_flag_name(enum unit_class_flag_id id, } if (user_class_flags[ufid].helptxt != nullptr) { - free(user_class_flags[ufid].helptxt); + delete[] user_class_flags[ufid].helptxt; user_class_flags[ufid].helptxt = nullptr; } @@ -1617,11 +1617,13 @@ void set_user_unit_type_flag_name(enum unit_type_flag_id id, int ufid = id - UTYF_USER_FLAG_1; fc_assert_ret(id >= UTYF_USER_FLAG_1 && id <= UTYF_LAST_USER_FLAG); - NFCN_FREE(user_type_flags[ufid].name); + delete[] user_type_flags[ufid].name; + user_type_flags[ufid].name = nullptr; if (name && name[0] != '\0') { user_type_flags[ufid].name = fc_strdup(name); } - NFCNPP_FREE(user_type_flags[ufid].helptxt); + delete[] user_type_flags[ufid].helptxt; + user_type_flags[ufid].helptxt = nullptr; if (helptxt && helptxt[0] != '\0') { user_type_flags[ufid].helptxt = fc_strdup(helptxt); } @@ -2101,7 +2103,11 @@ static void unit_type_free(struct unit_type *punittype) } veteran_system_destroy(punittype->veteran); - combat_bonus_list_iterate(punittype->bonuses, pbonus) { FC_FREE(pbonus); } + combat_bonus_list_iterate(punittype->bonuses, pbonus) + { + delete pbonus; + pbonus = nullptr; + } combat_bonus_list_iterate_end; combat_bonus_list_destroy(punittype->bonuses); } @@ -2267,7 +2273,8 @@ void unit_classes_free() if (unit_classes[i].cache.subset_movers != nullptr) { unit_class_list_destroy(unit_classes[i].cache.subset_movers); } - NFCN_FREE(unit_classes[i].helptext); + delete unit_classes[i].helptext; + unit_classes[i].helptext = nullptr; } } @@ -2355,7 +2362,7 @@ struct veteran_system *veteran_system_new(int count) // There must be at least one level. fc_assert_ret_val(count > 0, nullptr); - vsystem = new veteran_system[1](); + vsystem = new veteran_system{}; vsystem->levels = count; vsystem->definitions = new veteran_level[vsystem->levels](); @@ -2368,8 +2375,9 @@ struct veteran_system *veteran_system_new(int count) void veteran_system_destroy(struct veteran_system *vsystem) { if (vsystem) { - NFCPP_FREE(vsystem->definitions); - delete[] vsystem; + delete[] vsystem->definitions; + delete vsystem; + vsystem = nullptr; } } diff --git a/server/advisors/advdata.cpp b/server/advisors/advdata.cpp index 685b8bace1..32565d55d6 100644 --- a/server/advisors/advdata.cpp +++ b/server/advisors/advdata.cpp @@ -581,13 +581,19 @@ void adv_data_phase_done(struct player *pplayer) return; } - FCPP_FREE(adv->explore.ocean); - FCPP_FREE(adv->explore.continent); - FCPP_FREE(adv->threats.continent); - FCPP_FREE(adv->threats.ocean); - FCPP_FREE(adv->stats.cities); - FCPP_FREE(adv->stats.ocean_cities); - + delete[] adv->explore.ocean; + delete[] adv->explore.continent; + delete[] adv->threats.continent; + delete[] adv->threats.ocean; + delete[] adv->stats.cities; + delete[] adv->stats.ocean_cities; + + adv->explore.ocean = nullptr; + adv->explore.continent = nullptr; + adv->threats.continent = nullptr; + adv->threats.ocean = nullptr; + adv->stats.cities = nullptr; + adv->stats.ocean_cities = nullptr; adv->num_continents = 0; adv->num_oceans = 0; @@ -758,8 +764,8 @@ void adv_data_close(struct player *pplayer) adv_data_phase_done(pplayer); - NFCPP_FREE(adv->government_want); - + delete[] adv->government_want; + adv->government_want = nullptr; if (adv->dipl.adv_dipl_slots != nullptr) { players_iterate(aplayer) { @@ -772,7 +778,7 @@ void adv_data_close(struct player *pplayer) delete[] adv->dipl.adv_dipl_slots; } - NFC_FREE(adv); + delete adv; pplayer->server.adv = nullptr; } @@ -799,7 +805,7 @@ static void adv_dipl_free(const struct player *plr1, plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2); if (*dip_slot != nullptr) { - FC_FREE(*dip_slot); + delete *dip_slot; *dip_slot = nullptr; } } diff --git a/server/advisors/infracache.cpp b/server/advisors/infracache.cpp index 1e1de05083..0e04c06bea 100644 --- a/server/advisors/infracache.cpp +++ b/server/advisors/infracache.cpp @@ -520,6 +520,7 @@ void adv_city_free(struct city *pcity) free(pcity->server.adv->act_cache); // realloc pcity->server.adv->act_cache = nullptr; } - FCPP_FREE(pcity->server.adv); + delete[] pcity->server.adv; + pcity->server.adv = nullptr; } } diff --git a/server/cityhand.cpp b/server/cityhand.cpp index c04db22f3d..82c3ad6f81 100644 --- a/server/cityhand.cpp +++ b/server/cityhand.cpp @@ -544,7 +544,8 @@ void handle_city_rally_point(struct player *pplayer, int city_id, int length, pcity->rally_point.vigilant = false; pcity->rally_point.persistent = false; if (pcity->rally_point.orders) { - FCPP_FREE(pcity->rally_point.orders); + delete[] pcity->rally_point.orders; + pcity->rally_point.orders = nullptr; } } else { checked_orders = create_unit_orders(length, orders); @@ -578,7 +579,7 @@ void handle_city_manager(struct player *pplayer, int city_id, bool enabled, if (!enabled) { if (pcity->cm_parameter) { - free(pcity->cm_parameter); + delete pcity->cm_parameter; pcity->cm_parameter = nullptr; send_city_info(pplayer, pcity); } diff --git a/server/citytools.cpp b/server/citytools.cpp index a70f1e827b..6483b6c810 100644 --- a/server/citytools.cpp +++ b/server/citytools.cpp @@ -998,8 +998,10 @@ static void reestablish_city_trade_routes(struct city *pcity) trade_route_list_append(pcity->routes, proute); trade_route_list_append(partner->routes, back); } else { - free(proute); - free(back); + delete proute; + delete back; + proute = nullptr; + back = nullptr; // Now announce the traderoute removal announce_trade_route_removal(pcity, partner, false); @@ -1835,8 +1837,10 @@ void remove_city(struct city *pcity) struct trade_route *pback = remove_trade_route(pcity, proute, true, true); - FC_FREE(proute); - FC_FREE(pback); + delete proute; + delete pback; + proute = nullptr; + pback = nullptr; } trade_routes_iterate_safe_end; @@ -2232,7 +2236,8 @@ static void broadcast_city_info(struct city *pcity) traderoute_packet_list_iterate(routes, route_packet) { - FC_FREE(route_packet); + delete route_packet; + route_packet = nullptr; } traderoute_packet_list_iterate_end; traderoute_packet_list_destroy(routes); @@ -2421,7 +2426,8 @@ void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, if (routes != nullptr) { traderoute_packet_list_iterate(routes, route_packet) { - FC_FREE(route_packet); + delete route_packet; + route_packet = nullptr; } traderoute_packet_list_iterate_end; traderoute_packet_list_destroy(routes); @@ -3398,7 +3404,8 @@ void clear_worker_task(struct city *pcity, struct worker_task *ptask) packet.tgt = 0; packet.want = 0; - free(ptask); + delete ptask; + ptask = nullptr; lsend_packet_worker_task(city_owner(pcity)->connections, &packet); lsend_packet_worker_task(game.glob_observers, &packet); diff --git a/server/cityturn.cpp b/server/cityturn.cpp index 920ebe4360..41b8968884 100644 --- a/server/cityturn.cpp +++ b/server/cityturn.cpp @@ -645,8 +645,10 @@ void update_city_activities(struct player *pplayer) struct trade_route *back; back = remove_trade_route(pcity, proute, true, false); - free(proute); - free(back); + delete proute; + delete back; + proute = nullptr; + back = nullptr; } } } @@ -2634,7 +2636,8 @@ static bool city_build_unit(struct player *pplayer, struct city *pcity) pcity->rally_point.length = 0; pcity->rally_point.persistent = false; pcity->rally_point.vigilant = false; - FCPP_FREE(pcity->rally_point.orders); + delete[] pcity->rally_point.orders; + pcity->rally_point.orders = nullptr; } // Done building this unit; time to move on to the next. @@ -2714,7 +2717,8 @@ static bool sell_random_building(struct player *pplayer, city_refresh_queue_add(pcityimpr->pcity); - FC_FREE(pcityimpr); + delete pcityimpr; + pcityimpr = nullptr; return true; } @@ -2915,7 +2919,11 @@ player_balance_treasury_units_and_buildings(struct player *pplayer) } // Free remaining entries from list - cityimpr_list_iterate(pimprlist, pimpr) { FC_FREE(pimpr); } + cityimpr_list_iterate(pimprlist, pimpr) + { + delete pimpr; + pimpr = nullptr; + } cityimpr_list_iterate_end; if (pplayer->economic.gold < 0) { @@ -3006,7 +3014,11 @@ static bool city_balance_treasury_buildings(struct city *pcity) } // Free remaining entries from list - cityimpr_list_iterate(pimprlist, pimpr) { FC_FREE(pimpr); } + cityimpr_list_iterate(pimprlist, pimpr) + { + delete pimpr; + pimpr = nullptr; + } cityimpr_list_iterate_end; cityimpr_list_destroy(pimprlist); diff --git a/server/diplomats.cpp b/server/diplomats.cpp index b557c1d019..179d3491c4 100644 --- a/server/diplomats.cpp +++ b/server/diplomats.cpp @@ -338,7 +338,8 @@ bool diplomat_investigate(struct player *pplayer, struct unit *pdiplomat, traderoute_packet_list_iterate(routes, route_packet) { lsend_packet_traderoute_info(pplayer->connections, route_packet); - FC_FREE(route_packet); + delete route_packet; + route_packet = nullptr; } traderoute_packet_list_iterate_end; traderoute_packet_list_destroy(routes); diff --git a/server/edithand.cpp b/server/edithand.cpp index 81ed1de3cd..494ff9d04d 100644 --- a/server/edithand.cpp +++ b/server/edithand.cpp @@ -80,9 +80,7 @@ void edithand_init() need_continents_reassigned = false; - if (unfogged_players != nullptr) { - free(unfogged_players); - } + delete[] unfogged_players; unfogged_players = new bool[player_slot_count()](); } @@ -91,10 +89,8 @@ void edithand_init() */ void edithand_free() { - if (unfogged_players != nullptr) { - delete[] unfogged_players; - unfogged_players = nullptr; - } + delete[] unfogged_players; + unfogged_players = nullptr; } /** diff --git a/server/fcdb.cpp b/server/fcdb.cpp index 590ea376ed..94f5f01c86 100644 --- a/server/fcdb.cpp +++ b/server/fcdb.cpp @@ -90,8 +90,9 @@ static bool fcdb_set_option(const char *key, const char *value, if (removed) { /* Overwritten/removed an existing value */ fc_assert_ret_val(oldopt != nullptr, false); - FC_FREE(oldopt->value); + delete[] oldopt->value; delete oldopt; + oldopt = nullptr; } return true; @@ -174,8 +175,9 @@ void fcdb_free(void) for (auto popt : qAsConst(fcdb_config)) { // Dangling pointers freed below - FC_FREE(popt->value); + delete[] popt->value; delete popt; + popt = nullptr; } fcdb_config.clear(); } diff --git a/server/gamehand.cpp b/server/gamehand.cpp index e9bb76452d..c9e20a5f4b 100644 --- a/server/gamehand.cpp +++ b/server/gamehand.cpp @@ -720,7 +720,8 @@ void init_new_game() fc_assert(i == config.usable_startpos_num); } - free(config.startpos); + delete[] config.startpos; + config.startpos = nullptr; } } diff --git a/server/generator/mapgen.cpp b/server/generator/mapgen.cpp index e0e3226f3b..3e5ed6f3bc 100644 --- a/server/generator/mapgen.cpp +++ b/server/generator/mapgen.cpp @@ -1388,7 +1388,8 @@ bool map_fractal_generate(bool autosize, struct unit_type *initial_unit) || MAPGEN_FRACTAL == wld.map.server.generator || MAPGEN_FRACTURE == wld.map.server.generator) { make_land(); - FCPP_FREE(height_map); + delete[] height_map; + height_map = nullptr; } if (!wld.map.server.tinyisles) { remove_tiny_islands(); @@ -1674,9 +1675,8 @@ static struct terrain_select *tersel_new(int weight, */ static void tersel_free(struct terrain_select *ptersel) { - if (ptersel != nullptr) { - FC_FREE(ptersel); - } + delete ptersel; + ptersel = nullptr; } /** @@ -2335,7 +2335,8 @@ static void mapgenerator2() // init world created this map, destroy it before abort destroy_placed_map(); - FCPP_FREE(height_map); + delete[] height_map; + height_map = nullptr; return; } @@ -2351,7 +2352,8 @@ static void mapgenerator2() make_plains(); destroy_placed_map(); - FCPP_FREE(height_map); + delete[] height_map; + height_map = nullptr; if (checkmass > wld.map.xsize + wld.map.ysize + totalweight) { qDebug("%ld mass left unplaced", checkmass); @@ -2449,7 +2451,8 @@ static void mapgenerator3() make_plains(); destroy_placed_map(); - FCPP_FREE(height_map); + delete[] height_map; + height_map = nullptr; if (j == 1500) { qInfo(_("Generator 3 left %li landmass unplaced."), checkmass); @@ -2517,7 +2520,8 @@ static void mapgenerator4() } make_plains(); destroy_placed_map(); - FCPP_FREE(height_map); + delete[] height_map; + height_map = nullptr; if (checkmass > wld.map.xsize + wld.map.ysize + totalweight) { qDebug("%ld mass left unplaced", checkmass); diff --git a/server/generator/mapgen_utils.cpp b/server/generator/mapgen_utils.cpp index 286a7ddf9e..50087ec1c6 100644 --- a/server/generator/mapgen_utils.cpp +++ b/server/generator/mapgen_utils.cpp @@ -220,7 +220,8 @@ void smooth_int_map(int *int_map, bool zeroes_at_edges) target_map = int_map; } while (!axe); - FCPP_FREE(alt_int_map); + delete[] alt_int_map; + alt_int_map = nullptr; } /* These arrays are indexed by continent number (or negative of the diff --git a/server/generator/startpos.cpp b/server/generator/startpos.cpp index e620a67681..3aa2b3cc3a 100644 --- a/server/generator/startpos.cpp +++ b/server/generator/startpos.cpp @@ -531,15 +531,19 @@ bool create_start_positions(enum map_startpos mode, } } - FCPP_FREE(islands); - FCPP_FREE(islands_index); + delete[] islands; + delete[] islands_index; + islands = nullptr; + islands_index = nullptr; if (!is_tmap) { destroy_tmap(); } - FCPP_FREE(tile_value_aux); - FCPP_FREE(tile_value); + delete[] tile_value_aux; + delete[] tile_value; + tile_value_aux = nullptr; + tile_value = nullptr; return !failure; } diff --git a/server/generator/temperature_map.cpp b/server/generator/temperature_map.cpp index f75756c1b3..960ce6a58a 100644 --- a/server/generator/temperature_map.cpp +++ b/server/generator/temperature_map.cpp @@ -104,7 +104,8 @@ bool is_temperature_type_near(const struct tile *ptile, temperature_type tt) void destroy_tmap() { fc_assert_ret(nullptr != temperature_map); - FCPP_FREE(temperature_map); + delete[] temperature_map; + temperature_map = nullptr; } /** diff --git a/server/notify.cpp b/server/notify.cpp index 4b47c1b136..54ad0afb63 100644 --- a/server/notify.cpp +++ b/server/notify.cpp @@ -86,7 +86,8 @@ static void package_event_full(struct packet_chat_msg *packet, } if (is_capitalization_enabled()) { - FCPP_FREE(str); + delete[] str; + str = nullptr; } } @@ -870,21 +871,16 @@ void event_cache_load(struct section_file *file, const char *section) if (!valid && nullptr == players) { qDebug("[Event cache %4d] invalid target bitmap: %s", i, p); - if (nullptr != players) { - FC_FREE(players); - } + delete players; + players = nullptr; } } // insert event into the cache (void) event_cache_data_new(&packet, timestamp, server_status, target_type, players); - - if (nullptr != players) { - // free the event cache player selection - FC_FREE(players); - } - + delete players; + players = nullptr; qDebug("Event %4d loaded.", i); } } diff --git a/server/plrhand.cpp b/server/plrhand.cpp index 9fe7d9c1be..96687e8211 100644 --- a/server/plrhand.cpp +++ b/server/plrhand.cpp @@ -1828,8 +1828,10 @@ void server_remove_player(struct player *pplayer) struct trade_route *pback = remove_trade_route(pcity, proute, true, true); - FC_FREE(proute); - FC_FREE(pback); + delete proute; + delete pback; + proute = nullptr; + pback = nullptr; } trade_routes_iterate_safe_end; } diff --git a/server/report.cpp b/server/report.cpp index 5dfa8dd0e2..919c56564b 100644 --- a/server/report.cpp +++ b/server/report.cpp @@ -1125,7 +1125,8 @@ static void plrdata_slot_replace(struct plrdata_slot *plrdata, */ static void plrdata_slot_free(struct plrdata_slot *plrdata) { - NFCN_FREE(plrdata->name); + delete[] plrdata->name; + plrdata->name = nullptr; } /** @@ -1296,7 +1297,7 @@ void log_civ_score_init() { struct plrdata_slot *plrdata = score_log->plrdata + player_slot_index(pslot); - plrdata->name = nullptr; + plrdata_slot_free(plrdata); } player_slots_iterate_end; @@ -1323,13 +1324,14 @@ void log_civ_score_free() { struct plrdata_slot *plrdata = score_log->plrdata + player_slot_index(pslot); - NFC_FREE(plrdata->name); + plrdata_slot_free(plrdata); } player_slots_iterate_end; - free(score_log->plrdata); + delete[] score_log->plrdata; + score_log->plrdata = nullptr; } - free(score_log); + delete[] score_log; score_log = nullptr; } diff --git a/server/rscompat.cpp b/server/rscompat.cpp index 5af0aa71a6..d99fc9a99a 100644 --- a/server/rscompat.cpp +++ b/server/rscompat.cpp @@ -205,7 +205,8 @@ rscompat_enabler_add_obligatory_hard_reqs(struct action_enabler *ae) ae->disabled = new_enabler->disabled; requirement_vector_copy(&ae->actor_reqs, &new_enabler->actor_reqs); requirement_vector_copy(&ae->target_reqs, &new_enabler->target_reqs); - FC_FREE(new_enabler); + delete new_enabler; + new_enabler = nullptr; } else { // Register the new enabler action_enabler_add(new_enabler); diff --git a/server/ruleset.cpp b/server/ruleset.cpp index 836e753c91..514e0f32a3 100644 --- a/server/ruleset.cpp +++ b/server/ruleset.cpp @@ -506,7 +506,8 @@ static bool lookup_cbonus_list(struct rscompat_info *compat, if (!unit_type_flag_id_is_valid(bonus->flag)) { qCritical("\"%s\": unknown flag name \"%s\" in '%s.%s'.", filename, flag, sec, sub); - FC_FREE(bonus); + delete bonus; + bonus = nullptr; success = false; continue; } @@ -515,7 +516,8 @@ static bool lookup_cbonus_list(struct rscompat_info *compat, if (!combat_bonus_type_is_valid(bonus->type)) { qCritical("\"%s\": unknown bonus type \"%s\" in '%s.%s'.", filename, type, sec, sub); - FC_FREE(bonus); + delete bonus; + bonus = nullptr; success = false; continue; } @@ -523,7 +525,8 @@ static bool lookup_cbonus_list(struct rscompat_info *compat, j)) { qCritical("\"%s\": failed to get value from '%s.%s%d'.", filename, sec, sub, j); - FC_FREE(bonus); + delete bonus; + bonus = nullptr; success = false; continue; } @@ -620,7 +623,8 @@ static bool lookup_unit_list(struct section_file *file, const char *prefix, slist = secfile_lookup_str_vec(file, &nval, "%s.%s", prefix, entry); if (nval == 0) { // 'No vector' is considered same as empty vector - NFCPP_FREE(slist); + delete[] slist; + slist = nullptr; return true; } if (nval > MAX_NUM_UNIT_LIST) { @@ -630,6 +634,7 @@ static bool lookup_unit_list(struct section_file *file, const char *prefix, ok = false; } else if (nval == 1 && strcmp(slist[0], "") == 0) { delete[] slist; + slist = nullptr; return true; } if (ok) { @@ -650,6 +655,7 @@ static bool lookup_unit_list(struct section_file *file, const char *prefix, } } delete[] slist; + slist = nullptr; return ok; } @@ -678,7 +684,8 @@ static bool lookup_tech_list(struct section_file *file, const char *prefix, if (slist == nullptr) { return true; } else if (nval == 0) { - FCPP_FREE(slist); + delete[] slist; + slist = nullptr; return true; } else if (nval > MAX_NUM_TECH_LIST) { qCCritical(ruleset_category, @@ -689,7 +696,8 @@ static bool lookup_tech_list(struct section_file *file, const char *prefix, if (ok) { if (nval == 1 && strcmp(slist[0], "") == 0) { - FCPP_FREE(slist); + delete[] slist; + slist = nullptr; return true; } for (i = 0; i < nval && ok; i++) { @@ -715,7 +723,8 @@ static bool lookup_tech_list(struct section_file *file, const char *prefix, } } } - FCPP_FREE(slist); + delete[] slist; + slist = nullptr; return ok; } @@ -748,7 +757,8 @@ static bool lookup_building_list(struct section_file *file, ok = false; } else if (nval == 0 || (nval == 1 && strcmp(slist[0], "") == 0)) { if (slist != nullptr) { - FCPP_FREE(slist); + delete[] slist; + slist = nullptr; } return true; } @@ -769,6 +779,7 @@ static bool lookup_building_list(struct section_file *file, } } delete[] slist; + slist = nullptr; return ok; } @@ -886,6 +897,7 @@ static QVector *lookup_strvec(struct section_file *file, strvec_store(dest, vec, dim); delete[] vec; + vec = nullptr; return dest; } return nullptr; @@ -1363,6 +1375,7 @@ static bool load_ruleset_techs(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -1744,11 +1757,17 @@ static bool load_ruleset_veteran(struct section_file *file, const char *path, #undef rs_sanity_veteran } - NFCPP_FREE(vlist_name); - NFCPP_FREE(vlist_power); - NFCPP_FREE(vlist_raise); - NFCPP_FREE(vlist_wraise); - NFCPP_FREE(vlist_move); + delete[] vlist_name; + delete[] vlist_power; + delete[] vlist_raise; + delete[] vlist_wraise; + delete[] vlist_move; + + vlist_name = nullptr; + vlist_power = nullptr; + vlist_raise = nullptr; + vlist_wraise = nullptr; + vlist_move = nullptr; return ret; } @@ -1850,6 +1869,7 @@ static bool load_ruleset_units(struct section_file *file, } } delete[] slist; + slist = nullptr; uc->helptext = lookup_strvec(file, sec_name, "helptext"); @@ -2064,6 +2084,7 @@ static bool load_ruleset_units(struct section_file *file, BV_SET(u->cargo, uclass_index(uclass)); } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2086,6 +2107,7 @@ static bool load_ruleset_units(struct section_file *file, BV_SET(u->targets, uclass_index(uclass)); } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2108,6 +2130,7 @@ static bool load_ruleset_units(struct section_file *file, BV_SET(u->embarks, uclass_index(uclass)); } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2130,6 +2153,7 @@ static bool load_ruleset_units(struct section_file *file, BV_SET(u->disembarks, uclass_index(uclass)); } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2228,6 +2252,7 @@ static bool load_ruleset_units(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2264,6 +2289,7 @@ static bool load_ruleset_units(struct section_file *file, fc_assert(utype_has_role(u, ival)); } delete[] slist; + slist = nullptr; } unit_type_iterate_end; } @@ -2421,6 +2447,7 @@ static bool load_ruleset_buildings(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -2579,7 +2606,7 @@ static bool load_terrain_names(struct section_file *file, game.control.terrain_count = nval; // avoid re-reading files - NFCPP_FREE(terrain_sections); + delete[] terrain_sections; terrain_sections = new char[nval][MAX_SECTION_LABEL]{}; terrain_type_iterate(pterrain) @@ -2618,7 +2645,7 @@ static bool load_terrain_names(struct section_file *file, game.control.num_extra_types = nval; - NFCPP_FREE(extra_sections); + delete[] extra_sections; extra_sections = new char[nval][MAX_SECTION_LABEL]{}; if (ok) { @@ -2656,7 +2683,7 @@ static bool load_terrain_names(struct section_file *file, if (ok) { int idx; - NFCPP_FREE(base_sections); + delete[] base_sections; base_sections = new char[nval][MAX_SECTION_LABEL]{}; /* Cannot use base_type_iterate() before bases are added to @@ -2709,7 +2736,7 @@ static bool load_terrain_names(struct section_file *file, if (ok) { int idx; - NFCPP_FREE(road_sections); + delete[] road_sections; road_sections = new char[nval][MAX_SECTION_LABEL]{}; /* Cannot use extra_type_by_cause_iterate(EC_ROAD) before roads are added @@ -2762,7 +2789,7 @@ static bool load_terrain_names(struct section_file *file, if (ok) { int idx; - NFCPP_FREE(resource_sections); + delete[] resource_sections; resource_sections = new char[nval][MAX_SECTION_LABEL]{}; /* Cannot use resource_type_iterate() before resource are added to @@ -2947,7 +2974,8 @@ static bool load_ruleset_terrain(struct section_file *file, } } pterrain->resources[nval] = nullptr; - FCPP_FREE(res); + delete[] res; + res = nullptr; if (!ok) { break; @@ -3092,6 +3120,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3124,6 +3153,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3216,6 +3246,7 @@ static bool load_ruleset_terrain(struct section_file *file, } delete[] slist; + slist = nullptr; slist = secfile_lookup_str_vec(file, &nval, "%s.rmcauses", section); pextra->rmcauses = 0; @@ -3236,6 +3267,7 @@ static bool load_ruleset_terrain(struct section_file *file, } delete[] slist; + slist = nullptr; sz_strlcpy(pextra->activity_gfx, secfile_lookup_str_default(file, "-", "%s.activity_gfx", @@ -3362,6 +3394,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3385,6 +3418,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3408,6 +3442,7 @@ static bool load_ruleset_terrain(struct section_file *file, } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3430,6 +3465,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3454,6 +3490,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3637,6 +3674,7 @@ static bool load_ruleset_terrain(struct section_file *file, } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3696,6 +3734,9 @@ static bool load_ruleset_terrain(struct section_file *file, BV_SET(compat_bridged, pextra->id); } } + + delete[] slist; + slist = nullptr; } extra_type_by_cause_iterate_end; } @@ -3817,6 +3858,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -3854,6 +3896,7 @@ static bool load_ruleset_terrain(struct section_file *file, } } delete[] slist; + slist = nullptr; if (!ok) { break; @@ -4562,7 +4605,8 @@ load_city_name_list(struct section_file *file, struct nation_type *pnation, } while (nullptr != p && '\0' != *p); } } - NFCPP_FREE(cities); + delete[] cities; + cities = nullptr; return ok; } @@ -4603,6 +4647,7 @@ static bool load_ruleset_nations(struct section_file *file, } delete[] vec; + vec = nullptr; } game.default_government = nullptr; @@ -4648,6 +4693,7 @@ static bool load_ruleset_nations(struct section_file *file, } delete[] vec; + vec = nullptr; } vec = secfile_lookup_str_vec(file, &game.server.ruledit.at_count, @@ -4664,6 +4710,7 @@ static bool load_ruleset_nations(struct section_file *file, } delete[] vec; + vec = nullptr; } vec = secfile_lookup_str_vec(file, &game.server.ruledit.as_count, @@ -4680,6 +4727,7 @@ static bool load_ruleset_nations(struct section_file *file, } delete[] vec; + vec = nullptr; } sval = secfile_lookup_str_default(file, nullptr, @@ -4830,7 +4878,8 @@ static bool load_ruleset_nations(struct section_file *file, nation_rule_name(pnation), vec[j]); } } - NFCPP_FREE(vec); + delete[] vec; + vec = nullptr; if (nation_set_list_size(pnation->sets) < 1) { qCCritical(ruleset_category, "Nation %s is not a member of any nation set", @@ -4862,7 +4911,8 @@ static bool load_ruleset_nations(struct section_file *file, nation_rule_name(pnation), vec[j]); } } - NFCPP_FREE(vec); + delete[] vec; + vec = nullptr; if (!ok) { break; } @@ -5135,7 +5185,8 @@ static bool load_ruleset_nations(struct section_file *file, nation_rule_name(pnation), vec[j]); } } - NFCPP_FREE(vec); + delete[] vec; + vec = nullptr; if (!ok) { break; } @@ -5476,6 +5527,7 @@ static bool load_action_auto_uflag_block(struct section_file *file, } delete[] protecor_flag; + protecor_flag = nullptr; } return true; @@ -5512,6 +5564,7 @@ static bool load_action_auto_actions(struct section_file *file, } delete[] unit_acts; + unit_acts = nullptr; } return true; @@ -6113,7 +6166,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } else { // No summary if (game.ruleset_summary != nullptr) { - free(game.ruleset_summary); + delete[] game.ruleset_summary; game.ruleset_summary = nullptr; } } @@ -6130,7 +6183,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } else { // No description if (game.ruleset_description != nullptr) { - free(game.ruleset_description); + delete[] game.ruleset_description; game.ruleset_description = nullptr; } game.control.desc_length = 0; @@ -6194,6 +6247,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } } delete[] slist; + slist = nullptr; } if (ok) { @@ -6292,6 +6346,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } } delete[] food_ini; + food_ini = nullptr; } if (ok) { @@ -6438,7 +6493,8 @@ static bool load_ruleset_game(struct section_file *file, bool act, } if (psize) { - FCPP_FREE(protecor_flag); + delete[] protecor_flag; + protecor_flag = nullptr; } } } @@ -6617,6 +6673,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } delete[] quiet_actions; + quiet_actions = nullptr; } } @@ -6895,6 +6952,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, team_slot_set_defined_name(team_slot_by_number(i), svec[i]); } delete[] svec; + svec = nullptr; sec = secfile_sections_by_name_prefix(file, DISASTER_SECTION_PREFIX); nval = (nullptr != sec ? section_list_size(sec) : 0); @@ -6960,6 +7018,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } delete[] svec; + svec = nullptr; if (!ok) { break; @@ -7136,6 +7195,7 @@ static bool load_ruleset_game(struct section_file *file, bool act, } } delete[] slist; + slist = nullptr; pgood->helptext = lookup_strvec(file, sec_name, "helptext"); } @@ -8626,8 +8686,10 @@ static bool load_rulesetdir(const char *rsdir, bool compat_mode, playercolor_init(); game_ruleset_init(); - NFCNPP_FREE(script_buffer); - NFCNPP_FREE(parser_buffer); + delete[] script_buffer; + delete[] parser_buffer; + script_buffer = nullptr; + parser_buffer = nullptr; server.playable_nations = 0; @@ -8737,11 +8799,16 @@ static bool load_rulesetdir(const char *rsdir, bool compat_mode, nullcheck_secfile_destroy(effectfile); nullcheck_secfile_destroy(gamefile); - NFCNPP_FREE(extra_sections); - NFCNPP_FREE(base_sections); - NFCNPP_FREE(road_sections); - NFCNPP_FREE(resource_sections); - NFCNPP_FREE(terrain_sections); + delete[] extra_sections; + delete[] base_sections; + delete[] road_sections; + delete[] resource_sections; + delete[] terrain_sections; + extra_sections = nullptr; + base_sections = nullptr; + road_sections = nullptr; + resource_sections = nullptr; + terrain_sections = nullptr; if (ok) { rscompat_postprocess(&compat_info); diff --git a/server/savegame/savecompat.cpp b/server/savegame/savecompat.cpp index 29aaae0b12..a0aeddc465 100644 --- a/server/savegame/savecompat.cpp +++ b/server/savegame/savecompat.cpp @@ -406,7 +406,8 @@ static void compat_load_020400(struct loaddata *loading, * bogus so we trash them */ secfile_entry_delete(loading->file, "map.%s", newname); entry_set_name(pentry, newname); - FCPP_FREE(name2); + delete[] name2; + name2 = nullptr; } } entry_list_iterate_end; @@ -1926,7 +1927,8 @@ static void compat_load_dev(struct loaddata *loading) if (old_tgt_vec) { secfile_insert_int_vec(loading->file, old_tgt_vec, old_tgt_size, "player%d.u%d.sub_tgt_vec", plrno, unit); - free(old_tgt_vec); + delete[] old_tgt_vec; + old_tgt_vec = nullptr; } } } @@ -1970,7 +1972,7 @@ static void compat_load_dev(struct loaddata *loading) } } } - free(extra_vec); + delete[] extra_vec; } } } diff --git a/server/savegame/savegame2.cpp b/server/savegame/savegame2.cpp index 16621e8f3b..ea568bec7c 100644 --- a/server/savegame/savegame2.cpp +++ b/server/savegame/savegame2.cpp @@ -454,8 +454,7 @@ void savegame2_load(struct section_file *file) */ static struct loaddata *loaddata_new(struct section_file *file) { - struct loaddata *loading = - static_cast(calloc(1, sizeof(*loading))); + struct loaddata *loading = new loaddata{}; loading->file = file; loading->secfile_options = nullptr; @@ -494,19 +493,20 @@ static struct loaddata *loaddata_new(struct section_file *file) */ static void loaddata_destroy(struct loaddata *loading) { - NFC_FREE(loading->improvement.order); - NFC_FREE(loading->technology.order); - NFC_FREE(loading->activities.order); - NFC_FREE(loading->trait.order); - NFC_FREE(loading->extra.order); - NFC_FREE(loading->multiplier.order); - NFC_FREE(loading->special.order); - NFC_FREE(loading->base.order); - NFC_FREE(loading->road.order); - NFC_FREE(loading->specialist.order); - NFC_FREE(loading->ds_t.order); - NFC_FREE(loading->worked_tiles); - free(loading); + delete[] loading->improvement.order; + delete[] loading->technology.order; + delete[] loading->activities.order; + delete[] loading->trait.order; + delete[] loading->extra.order; + delete[] loading->multiplier.order; + delete[] loading->special.order; + delete[] loading->base.order; + delete[] loading->road.order; + delete[] loading->specialist.order; + delete[] loading->ds_t.order; + delete[] loading->worked_tiles; + delete loading; + loading = nullptr; } /* ======================================================================= @@ -2298,7 +2298,8 @@ static void sg_load_map_known(struct loaddata *loading) } whole_map_iterate_end; - FCPP_FREE(known); + delete[] known; + known = nullptr; } } @@ -4231,7 +4232,8 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, || (order->order == ORDER_ACTIVITY && order->activity == ACTIVITY_LAST)) { // An invalid order. Just drop the orders for this unit. - FCPP_FREE(punit->orders.list); + delete[] punit->orders.list; + punit->orders.list = nullptr; punit->has_orders = false; break; } @@ -4383,7 +4385,7 @@ static void sg_load_player_attributes(struct loaddata *loading, // Toss any existing attribute_block (should not exist) if (plr->attribute_block.data) { - free(plr->attribute_block.data); + delete[] plr->attribute_block.data; plr->attribute_block.data = nullptr; } @@ -4416,7 +4418,7 @@ static void sg_load_player_attributes(struct loaddata *loading, quoted = new char[quoted_length + 1]; quoted[0] = '\0'; - plr->attribute_block.data = fc_malloc(plr->attribute_block.length); + plr->attribute_block.data = new char[plr->attribute_block.length]{}; for (part_nr = 0; part_nr < parts; part_nr++) { const char *current = secfile_lookup_str( loading->file, "player%d.attribute_v2_block_data.part%d", plrno, diff --git a/server/savegame/savegame3.cpp b/server/savegame/savegame3.cpp index 28d8ff55ef..fee9017349 100644 --- a/server/savegame/savegame3.cpp +++ b/server/savegame/savegame3.cpp @@ -561,7 +561,7 @@ static void savegame3_save_real(struct section_file *file, */ static struct loaddata *loaddata_new(struct section_file *file) { - struct loaddata *loading = new loaddata[1](); + struct loaddata *loading = new loaddata{}; loading->file = file; loading->secfile_options = nullptr; @@ -598,18 +598,19 @@ static struct loaddata *loaddata_new(struct section_file *file) */ static void loaddata_destroy(struct loaddata *loading) { - NFCPP_FREE(loading->improvement.order); - NFCPP_FREE(loading->technology.order); - NFCPP_FREE(loading->activities.order); - NFCPP_FREE(loading->trait.order); - NFCPP_FREE(loading->extra.order); - NFCPP_FREE(loading->multiplier.order); - NFCPP_FREE(loading->specialist.order); - NFCPP_FREE(loading->action.order); - NFCPP_FREE(loading->act_dec.order); - NFCPP_FREE(loading->ssa.order); - NFCPP_FREE(loading->worked_tiles); - delete[] loading; + delete[] loading->improvement.order; + delete[] loading->technology.order; + delete[] loading->activities.order; + delete[] loading->trait.order; + delete[] loading->extra.order; + delete[] loading->multiplier.order; + delete[] loading->specialist.order; + delete[] loading->action.order; + delete[] loading->act_dec.order; + delete[] loading->ssa.order; + delete[] loading->worked_tiles; + delete loading; + loading = nullptr; } /** @@ -5000,7 +5001,8 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, if (rally_orders[i] == '\0' || rally_dirs[i] == '\0' || rally_activities[i] == '\0') { log_sg("Invalid rally point."); - FCPP_FREE(pcity->rally_point.orders); + delete[] pcity->rally_point.orders; + pcity->rally_point.orders = nullptr; pcity->rally_point.length = 0; break; } @@ -5057,7 +5059,7 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, bool enabled = secfile_lookup_bool_default(loading->file, false, "%s.cma_enabled", citystr); if (enabled) { - struct cm_parameter *param = new cm_parameter[1](); + struct cm_parameter *param = new cm_parameter(); for (i = 0; i < O_LAST; i++) { param->minimal_surplus[i] = secfile_lookup_int_default( @@ -6000,7 +6002,8 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, || (order->order == ORDER_ACTIVITY && order->activity == ACTIVITY_LAST)) { // An invalid order. Just drop the orders for this unit. - FCPP_FREE(punit->orders.list); + delete[] punit->orders.list; + punit->orders.list = nullptr; punit->has_orders = false; break; } @@ -6083,7 +6086,8 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, && unit_activity_needs_target_from_client(act) && order->sub_target == EXTRA_NONE) { // Missing required action extra target. - FCPP_FREE(punit->orders.list); + delete[] punit->orders.list; + punit->orders.list = nullptr; punit->has_orders = false; } } else if (order->order != ORDER_PERFORM_ACTION) { @@ -6431,7 +6435,7 @@ static void sg_load_player_attributes(struct loaddata *loading, // Toss any existing attribute_block (should not exist) if (plr->attribute_block.data) { - free(plr->attribute_block.data); + delete[] plr->attribute_block.data; plr->attribute_block.data = nullptr; } @@ -6464,7 +6468,7 @@ static void sg_load_player_attributes(struct loaddata *loading, quoted = new char[quoted_length + 1]; quoted[0] = '\0'; - plr->attribute_block.data = fc_malloc(plr->attribute_block.length); + plr->attribute_block.data = new char[plr->attribute_block.length]{}; for (part_nr = 0; part_nr < parts; part_nr++) { const char *current = secfile_lookup_str( loading->file, "player%d.attribute_v2_block_data.part%d", plrno, @@ -7123,7 +7127,8 @@ static void sg_load_researches(struct loaddata *loading) presearch->inventions[o].bulbs_researched_saved = vlist_research[o]; } advance_index_iterate_end; - NFCPP_FREE(vlist_research); + delete[] vlist_research; + vlist_research = nullptr; } } @@ -7170,7 +7175,8 @@ static void sg_save_researches(struct savedata *saving) secfile_insert_int_vec(saving->file, vlist_research, game.control.num_tech_types, "research.r%d.vbs", i); - NFCPP_FREE(vlist_research); + delete[] vlist_research; + vlist_research = nullptr; } technology_save(saving->file, "research.r%d.saved", i, presearch->researching_saved); diff --git a/server/scripting/script_server.cpp b/server/scripting/script_server.cpp index ea2ba0de4e..a494469b86 100644 --- a/server/scripting/script_server.cpp +++ b/server/scripting/script_server.cpp @@ -246,7 +246,8 @@ static void script_server_code_init() { script_server_code = nullptr; } static void script_server_code_free() { if (script_server_code) { - FCPP_FREE(script_server_code); + delete[] script_server_code; + script_server_code = nullptr; } } diff --git a/server/sernet.cpp b/server/sernet.cpp index 81ceb3742c..6345d49f62 100644 --- a/server/sernet.cpp +++ b/server/sernet.cpp @@ -86,7 +86,8 @@ static void close_connection(struct connection *pconn) while (!pconn->server.ping_timers->isEmpty()) { timer_destroy(pconn->server.ping_timers->takeFirst()); } - FC_FREE(pconn->server.ping_timers); + delete pconn->server.ping_timers; + pconn->server.ping_timers = nullptr; } conn_pattern_list_destroy(pconn->server.ignore_list); diff --git a/server/stdinhand.cpp b/server/stdinhand.cpp index 32b792b9e6..dd8746118a 100644 --- a/server/stdinhand.cpp +++ b/server/stdinhand.cpp @@ -1733,7 +1733,8 @@ static void show_help_option(struct connection *caller, fc_break_lines(help, LINE_BREAK); cmd_reply(help_cmd, caller, C_COMMENT, _("Description:")); cmd_reply_prefix(help_cmd, caller, C_COMMENT, " ", " %s", help); - FCPP_FREE(help); + delete[] help; + help = nullptr; } cmd_reply(help_cmd, caller, C_COMMENT, _("Status: %s"), (setting_is_changeable(pset, nullptr, nullptr, 0) diff --git a/server/unittools.cpp b/server/unittools.cpp index 626964dd3e..c28890dd39 100644 --- a/server/unittools.cpp +++ b/server/unittools.cpp @@ -3248,7 +3248,8 @@ static bool unit_survive_autoattack(struct unit *punit) probability->unit_id = penemy->id; autoattack_prob_list_prepend(autoattack, probability); } else { - FC_FREE(probability); + delete probability; + probability = nullptr; } } unit_list_iterate_end; diff --git a/tools/ruledit/tab_nation.cpp b/tools/ruledit/tab_nation.cpp index ec44b235b0..29c32f36c2 100644 --- a/tools/ruledit/tab_nation.cpp +++ b/tools/ruledit/tab_nation.cpp @@ -82,7 +82,8 @@ void tab_nation::refresh() */ void tab_nation::flush_widgets() { - FC_FREE(ui->data.nationlist); + delete[] ui->data.nationlist; + ui->data.nationlist = nullptr; if (via_include->isChecked()) { QByteArray nln_bytes; @@ -108,7 +109,7 @@ void tab_nation::nationlist_toggle(bool checked) } else { QByteArray nln_bytes; - FC_FREE(ui->data.nationlist_saved); + delete[] ui->data.nationlist_saved; nln_bytes = nationlist->text().toUtf8(); ui->data.nationlist_saved = fc_strdup(nln_bytes.data()); ui->data.nationlist = nullptr; diff --git a/tools/ruleutil/rulesave.cpp b/tools/ruleutil/rulesave.cpp index 938a4f1c3e..1d69ededc8 100644 --- a/tools/ruleutil/rulesave.cpp +++ b/tools/ruleutil/rulesave.cpp @@ -1786,7 +1786,8 @@ static bool save_nation(struct section_file *sfile, struct nation_type *pnat, secfile_insert_str_vec(sfile, list_items, set_count, "%s.cities", path); for (i = 0; i < set_count; i++) { - FC_FREE(city_str[i]); + delete[] city_str[i]; + city_str[i] = nullptr; } } diff --git a/utility/fcintl.cpp b/utility/fcintl.cpp index 86d62b5d6d..9f1d6e24a2 100644 --- a/utility/fcintl.cpp +++ b/utility/fcintl.cpp @@ -74,7 +74,11 @@ char *capitalized_string(const char *str) /** Free capitalized string. */ -void free_capitalized(char *str) { FC_FREE(str); } +void free_capitalized(char *str) +{ + delete[] str; + str = nullptr; +} /** Translation opts in to automatic capitalization features. diff --git a/utility/genhash.cpp b/utility/genhash.cpp index 2fe84f7b6d..5b75be86e7 100644 --- a/utility/genhash.cpp +++ b/utility/genhash.cpp @@ -291,6 +291,7 @@ void genhash_destroy(struct genhash *pgenhash) genhash_clear(pgenhash); delete[] pgenhash->buckets; delete pgenhash; + pgenhash = nullptr; } /** diff --git a/utility/log.cpp b/utility/log.cpp index dcffc96cb7..db3e4546fc 100644 --- a/utility/log.cpp +++ b/utility/log.cpp @@ -193,9 +193,7 @@ void log_set_file(const QString &path) } // Unset the old one - if (log_file != nullptr) { - delete log_file; - } + delete log_file; log_file = new_file; } @@ -213,7 +211,8 @@ void log_close() { QMutexLocker locker(&mutex); - NFCN_FREE(log_file); + delete log_file; + log_file = nullptr; // Reinstall the old handler qInstallMessageHandler(original_handler); } diff --git a/utility/registry_ini.cpp b/utility/registry_ini.cpp index d880a52198..8ea76772d5 100644 --- a/utility/registry_ini.cpp +++ b/utility/registry_ini.cpp @@ -3180,8 +3180,9 @@ void entry_destroy(struct entry *pentry) // Common free. delete[] pentry->name; - NFCPP_FREE(pentry->comment); + delete[] pentry->comment; delete pentry; + pentry = nullptr; } /** diff --git a/utility/section_file.cpp b/utility/section_file.cpp index 1593b5b2ad..f5cfa6020d 100644 --- a/utility/section_file.cpp +++ b/utility/section_file.cpp @@ -93,11 +93,13 @@ void secfile_destroy(struct section_file *secfile) /* Mark it nullptr to be sure to don't try to make operations when * deleting the entries. */ secfile->hash.sections = nullptr; - NFCN_FREE(secfile->hash.entries); + delete secfile->hash.entries; + secfile->hash.entries = nullptr; section_list_destroy(secfile->sections); - NFCPP_FREE(secfile->name); - + delete[] secfile->name; + secfile->name = nullptr; delete secfile; + secfile = nullptr; } /** diff --git a/utility/shared.cpp b/utility/shared.cpp index 4b345319aa..b8ed748b81 100644 --- a/utility/shared.cpp +++ b/utility/shared.cpp @@ -1172,8 +1172,10 @@ void init_nls() */ void free_nls() { - FCPP_FREE(grouping); - FCPP_FREE(grouping_sep); + delete[] grouping; + delete[] grouping_sep; + grouping = nullptr; + grouping_sep = nullptr; } /** @@ -1328,7 +1330,11 @@ char *get_multicast_group(bool ipv6_preferred) /** Free multicast group resources */ -void free_multicast_group() { NFCNPP_FREE(mc_group); } +void free_multicast_group() +{ + delete[] mc_group; + mc_group = nullptr; +} /** Interpret ~/ in filename as home dir diff --git a/utility/timing.cpp b/utility/timing.cpp index c4881bf69f..5672d6aa90 100644 --- a/utility/timing.cpp +++ b/utility/timing.cpp @@ -66,7 +66,11 @@ civtimer *timer_renew(civtimer *t, enum timer_timetype type, /** Deletes timer */ -void timer_destroy(civtimer *t) { NFC_FREE(t); } +void timer_destroy(civtimer *t) +{ + delete t; + t = nullptr; +} /** Return whether timer is in use.