From 2c1b7410f3e7f2a50ea3bd8defd687b010a9a910 Mon Sep 17 00:00:00 2001 From: yexo Date: Thu, 25 Feb 2010 15:54:40 +0000 Subject: [PATCH] (svn r19251) -Codechange: add a contructor to GRFError and use it to allocating errors more uniform. -Fix: some grf error messages didn't free the previous error messages, creating a memory leak --- src/newgrf.cpp | 52 +++++++++++++------------------------------ src/newgrf_config.cpp | 23 ++++++++++++------- src/newgrf_config.h | 6 ++++- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index ba322df5ce..194670ec5a 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2756,8 +2756,8 @@ static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uin /* No debug message for an invalid ID, as it has already been output */ _skip_sprites = -1; _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL); _cur_grfconfig->error->message = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY; return true; } @@ -4331,16 +4331,10 @@ static void CfgApply(ByteReader *buf) */ static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c) { - if (c->error != NULL) { - free(c->error->custom_message); - free(c->error->data); - free(c->error); - } + delete c->error; c->status = GCS_DISABLED; - c->error = CallocT(1); + c->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC); c->error->data = strdup(_cur_grfconfig->name); - c->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - c->error->message = STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC; ClearTemporaryNewGRFData(GetFileByGRFID(c->grfid)); } @@ -4549,9 +4543,8 @@ static void GRFInfo(ByteReader *buf) if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) { _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - _cur_grfconfig->error->message = STR_NEWGRF_ERROR_MULTIPLE_ACTION_8; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_MULTIPLE_ACTION_8); _skip_sprites = -1; return; @@ -4689,9 +4682,7 @@ static void GRFLoadError(ByteReader *buf) return; } - GRFError *error = CallocT(1); - - error->severity = sevstr[severity]; + GRFError *error = new GRFError(sevstr[severity]); if (message_id == 0xFF) { /* This is a custom error message. */ @@ -5559,17 +5550,12 @@ static void TranslateGRFStrings(ByteReader *buf) if (c->status == GCS_INITIALISED) { /* If the file is not active but will be activated later, give an error * and disable this file. */ - GRFError *error = CallocT(1); + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_LOAD_AFTER); char tmp[256]; GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp)); - error->data = strdup(tmp); - - error->message = STR_NEWGRF_ERROR_LOAD_AFTER; - error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - - if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error); - _cur_grfconfig->error = error; + _cur_grfconfig->error->data = strdup(tmp); _cur_grfconfig->status = GCS_DISABLED; ClearTemporaryNewGRFData(_cur_grffile); @@ -5862,9 +5848,7 @@ static void ResetNewGRFErrors() { for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { if (!HasBit(c->flags, GCF_COPY) && c->error != NULL) { - free(c->error->custom_message); - free(c->error->data); - free(c->error); + delete c->error; c->error = NULL; } } @@ -6361,9 +6345,8 @@ static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage) _skip_sprites = -1; _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - _cur_grfconfig->error->message = STR_NEWGRF_ERROR_READ_BOUNDS; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_READ_BOUNDS); } } @@ -6393,9 +6376,7 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage) if (file_index > LAST_GRF_SLOT) { DEBUG(grf, 0, "'%s' is not loaded as the maximum number of GRFs has been reached", filename); config->status = GCS_DISABLED; - config->error = CallocT(1); - config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - config->error->message = STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED; + config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED); return; } @@ -6441,9 +6422,8 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage) if (_skip_sprites == 0) { grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling"); config->status = GCS_DISABLED; - config->error = CallocT(1); - config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - config->error->message = STR_NEWGRF_ERROR_UNEXPECTED_SPRITE; + delete config->error; + config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_UNEXPECTED_SPRITE); break; } diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index c996c3f049..28e67e7f50 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -26,6 +26,17 @@ GRFConfig *_grfconfig; GRFConfig *_grfconfig_newgame; GRFConfig *_grfconfig_static; +GRFError::GRFError(StringID severity, StringID message) : + message(message), + severity(severity) +{ +} + +GRFError::~GRFError() +{ + free(this->custom_message); + free(this->data); + } /** * Update the palettes of the graphics from the config file. @@ -101,12 +112,7 @@ void ClearGRFConfig(GRFConfig **config) free((*config)->filename); free((*config)->name); free((*config)->info); - - if ((*config)->error != NULL) { - free((*config)->error->custom_message); - free((*config)->error->data); - free((*config)->error); - } + delete (*config)->error; } free(*config); *config = NULL; @@ -139,8 +145,9 @@ GRFConfig *DuplicateGRFConfig(const GRFConfig *c) if (c->name != NULL) config->name = strdup(c->name); if (c->info != NULL) config->info = strdup(c->info); if (c->error != NULL) { - config->error = MallocT(1); - memcpy(config->error, c->error, sizeof(GRFError)); + config->error = new GRFError(c->error->severity, c->error->message); + config->error->num_params = c->error->num_params; + memcpy(config->error->param_value, c->error->param_value, sizeof(config->error->param_value)); if (c->error->data != NULL) config->error->data = strdup(c->error->data); if (c->error->custom_message != NULL) config->error->custom_message = strdup(c->error->custom_message); } diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 74e2d54ab6..fe3eab37ed 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -13,6 +13,7 @@ #define NEWGRF_CONFIG_H #include "strings_type.h" +#include "core/alloc_type.hpp" /** GRF config bit flags */ enum GCF_Flags { @@ -55,7 +56,10 @@ struct GRFIdentifier { }; /** Information about why GRF had problems during initialisation */ -struct GRFError { +struct GRFError : ZeroedMemoryAllocator { + GRFError(StringID severity, StringID message = 0); + ~GRFError(); + char *custom_message; ///< Custom message (if present) char *data; ///< Additional data for message and custom_message StringID message; ///< Default message