From bcc205d8b68e9276d4901e1b456b5c0808ec3c1c Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 27 Mar 2025 19:16:06 +0000 Subject: [PATCH] Codechange: Store GameStrings as shared_ptr. Uses shared_ptr/weak_ptr as LanguageScanner needs access without ownership. --- src/game/game_text.cpp | 20 +++++++++++--------- src/saveload/game_sl.cpp | 8 +++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index 369f42803a..8e0a4e1132 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -168,12 +168,12 @@ struct StringNameWriter : HeaderWriter { */ class LanguageScanner : protected FileScanner { private: - GameStrings *gs; + std::weak_ptr gs; std::string exclude; public: /** Initialise */ - LanguageScanner(GameStrings *gs, const std::string &exclude) : gs(gs), exclude(exclude) {} + LanguageScanner(std::weak_ptr gs, const std::string &exclude) : gs(gs), exclude(exclude) {} /** * Scan. @@ -190,8 +190,12 @@ public: auto ls = ReadRawLanguageStrings(filename); if (!ls.IsValid()) return false; - gs->raw_strings.push_back(std::move(ls)); - return true; + if (auto sp = this->gs.lock()) { + sp->raw_strings.push_back(std::move(ls)); + return true; + } + + return false; } }; @@ -199,7 +203,7 @@ public: * Load all translations that we know of. * @return Container with all (compiled) translations. */ -GameStrings *LoadTranslations() +static std::shared_ptr LoadTranslations() { const GameInfo *info = Game::GetInfo(); assert(info != nullptr); @@ -214,7 +218,7 @@ GameStrings *LoadTranslations() auto ls = ReadRawLanguageStrings(filename); if (!ls.IsValid()) return nullptr; - GameStrings *gs = new GameStrings(); + auto gs = std::make_shared(); try { gs->raw_strings.push_back(std::move(ls)); @@ -245,7 +249,6 @@ GameStrings *LoadTranslations() gs->Compile(); return gs; } catch (...) { - delete gs; return nullptr; } } @@ -308,7 +311,7 @@ void GameStrings::Compile() } /** The currently loaded game strings. */ -GameStrings *_current_data = nullptr; +std::shared_ptr _current_data = nullptr; /** * Get the string pointer of a particular game string. @@ -355,7 +358,6 @@ const std::string &GetGameStringName(StringIndexInTab id) */ void RegisterGameTranslation(Squirrel *engine) { - delete _current_data; _current_data = LoadTranslations(); if (_current_data == nullptr) return; diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index fdfce16c68..af9512c22b 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -113,7 +113,7 @@ struct GSDTChunkHandler : ChunkHandler { } }; -extern GameStrings *_current_data; +extern std::shared_ptr _current_data; static std::string _game_saveload_string; static uint32_t _game_saveload_strings; @@ -159,8 +159,7 @@ struct GSTRChunkHandler : ChunkHandler { { const std::vector slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat); - delete _current_data; - _current_data = new GameStrings(); + _current_data = std::make_shared(); while (SlIterateArray() != -1) { LanguageStrings ls; @@ -170,8 +169,7 @@ struct GSTRChunkHandler : ChunkHandler { /* If there were no strings in the savegame, set GameStrings to nullptr */ if (_current_data->raw_strings.empty()) { - delete _current_data; - _current_data = nullptr; + _current_data.reset(); return; }