diff --git a/src/fios.h b/src/fios.h index 0d9b170541..d5e0b9ff7a 100644 --- a/src/fios.h +++ b/src/fios.h @@ -32,7 +32,7 @@ typedef SmallMap CompanyPropertiesMap; struct LoadCheckData { bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.) StringID error; ///< Error message from loading. INVALID_STRING_ID if no error. - char *error_data; ///< Data to pass to SetDParamStr when displaying #error. + std::string error_msg; ///< Data to pass to SetDParamStr when displaying #error. uint32 map_size_x, map_size_y; TimerGameCalendar::Date current_date; @@ -47,7 +47,7 @@ struct LoadCheckData { struct LoggedAction *gamelog_action; ///< Gamelog actions uint gamelog_actions; ///< Number of gamelog actions - LoadCheckData() : error_data(nullptr), grfconfig(nullptr), + LoadCheckData() : grfconfig(nullptr), grf_compatibility(GLC_NOT_FOUND), gamelog_action(nullptr), gamelog_actions(0) { this->Clear(); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 11d74b5e4c..c358e4a597 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -49,8 +49,7 @@ void LoadCheckData::Clear() { this->checkable = false; this->error = INVALID_STRING_ID; - free(this->error_data); - this->error_data = nullptr; + this->error_msg.clear(); this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize. this->current_date = 0; @@ -500,7 +499,7 @@ public: tr.top += FONT_HEIGHT_NORMAL; } else if (_load_check_data.error != INVALID_STRING_ID) { /* Incompatible / broken savegame */ - SetDParamStr(0, _load_check_data.error_data); + SetDParamStr(0, _load_check_data.error_msg); tr.top = DrawStringMultiLine(tr, _load_check_data.error, TC_RED); } else { /* Mapsize */ diff --git a/src/openttd.cpp b/src/openttd.cpp index e919e80b66..380dcb3b4e 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -623,7 +623,7 @@ int openttd_main(int argc, char *argv[]) if (_load_check_data.HasErrors()) { InitializeLanguagePacks(); // A language pack is needed for GetString() char buf[256]; - SetDParamStr(0, _load_check_data.error_data); + SetDParamStr(0, _load_check_data.error_msg); GetString(buf, _load_check_data.error, lastof(buf)); fprintf(stderr, "%s\n", buf); } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 0632451a71..0dc1baa10d 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -208,7 +208,7 @@ struct SaveLoadParams { LoadFilter *lf; ///< Filter to read the savegame from. StringID error_str; ///< the translatable error message to show - char *extra_msg; ///< the error message + std::string extra_msg; ///< the error message bool saveinprogress; ///< Whether there is currently a save in progress. }; @@ -330,17 +330,15 @@ static void SlNullPointers() * @note This function does never return as it throws an exception to * break out of all the saveload code. */ -void NORETURN SlError(StringID string, const char *extra_msg) +void NORETURN SlError(StringID string, const std::string &extra_msg) { /* Distinguish between loading into _load_check_data vs. normal save/load. */ if (_sl.action == SLA_LOAD_CHECK) { _load_check_data.error = string; - free(_load_check_data.error_data); - _load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg); + _load_check_data.error_msg = extra_msg; } else { _sl.error_str = string; - free(_sl.extra_msg); - _sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg); + _sl.extra_msg = extra_msg; } /* We have to nullptr all pointers here; we might be in a state where @@ -362,7 +360,7 @@ void NORETURN SlError(StringID string, const char *extra_msg) * @note This function does never return as it throws an exception to * break out of all the saveload code. */ -void NORETURN SlErrorCorrupt(const char *msg) +void NORETURN SlErrorCorrupt(const std::string &msg) { SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg); } diff --git a/src/saveload/saveload_error.hpp b/src/saveload/saveload_error.hpp index 77025f5a33..5a42257daa 100644 --- a/src/saveload/saveload_error.hpp +++ b/src/saveload/saveload_error.hpp @@ -13,8 +13,8 @@ #include "../3rdparty/fmt/format.h" #include "../strings_type.h" -void NORETURN SlError(StringID string, const char *extra_msg = nullptr); -void NORETURN SlErrorCorrupt(const char *msg); +void NORETURN SlError(StringID string, const std::string &extra_msg = {}); +void NORETURN SlErrorCorrupt(const std::string &msg); /** * Issue an SlErrorCorrupt with a format string. @@ -28,7 +28,7 @@ void NORETURN SlErrorCorrupt(const char *msg); template static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args) { - SlErrorCorrupt(fmt::format(format, fmt_args...).c_str()); + SlErrorCorrupt(fmt::format(format, fmt_args...)); } #endif /* SAVELOAD_ERROR_HPP */