(svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized

pull/155/head
yexo 14 years ago
parent b21278659c
commit daf89297fe

@ -178,8 +178,7 @@ static void LoadSpriteTables()
* so we have to manually add it, and then remove it later.
*/
GRFConfig *top = _grfconfig;
GRFConfig *master = CallocT<GRFConfig>(1);
master->filename = strdup(used_set->files[GFT_EXTRA].filename);
GRFConfig *master = new GRFConfig(used_set->files[GFT_EXTRA].filename);
FillGRFDetails(master, false);
master->windows_paletted = (used_set->palette == PAL_WINDOWS);
ClrBit(master->flags, GCF_INIT_ONLY);
@ -189,7 +188,7 @@ static void LoadSpriteTables()
LoadNewGRF(SPR_NEWGRFS_BASE, i);
/* Free and remove the top element. */
ClearGRFConfig(&master);
delete master;
_grfconfig = top;
}

@ -238,7 +238,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
for (i = 0; i < num_grfs; i++) {
GRFConfig *c = CallocT<GRFConfig>(1);
GRFConfig *c = new GRFConfig();
this->Recv_GRFIdentifier(p, &c->ident);
this->HandleIncomingNetworkGameInfoGRFConfig(c);

@ -20,6 +20,21 @@
#include "fileio_func.h"
#include "fios.h"
GRFConfig::GRFConfig(const char *filename)
{
if (filename != NULL) this->filename = strdup(filename);
}
GRFConfig::~GRFConfig()
{
/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
if (!HasBit(this->flags, GCF_COPY)) {
free(this->filename);
free(this->name);
free(this->info);
delete this->error;
}
}
GRFConfig *_all_grfs;
GRFConfig *_grfconfig;
@ -105,27 +120,13 @@ bool FillGRFDetails(GRFConfig *config, bool is_static)
}
void ClearGRFConfig(GRFConfig **config)
{
/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
if (!HasBit((*config)->flags, GCF_COPY)) {
free((*config)->filename);
free((*config)->name);
free((*config)->info);
delete (*config)->error;
}
free(*config);
*config = NULL;
}
/* Clear a GRF Config list */
void ClearGRFConfigList(GRFConfig **config)
{
GRFConfig *c, *next;
for (c = *config; c != NULL; c = next) {
next = c->next;
ClearGRFConfig(&c);
delete c;
}
*config = NULL;
}
@ -138,7 +139,7 @@ void ClearGRFConfigList(GRFConfig **config)
*/
GRFConfig *DuplicateGRFConfig(const GRFConfig *c)
{
GRFConfig *config = MallocT<GRFConfig>(1);
GRFConfig *config = new GRFConfig();
*config = *c;
if (c->filename != NULL) config->filename = strdup(c->filename);
@ -203,7 +204,7 @@ static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
if (cur->ident.grfid != list->ident.grfid) continue;
prev->next = cur->next;
ClearGRFConfig(&cur);
delete cur;
cur = prev; // Just go back one so it continues as normal later on
}
@ -320,8 +321,7 @@ public:
bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
{
GRFConfig *c = CallocT<GRFConfig>(1);
c->filename = strdup(filename + basepath_length);
GRFConfig *c = new GRFConfig(filename + basepath_length);
bool added = true;
if (FillGRFDetails(c, false)) {
@ -355,7 +355,7 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
if (!added) {
/* File couldn't be opened, or is either not a NewGRF or is a
* 'system' NewGRF or it's already known, so forget about it. */
ClearGRFConfig(&c);
delete c;
}
return added;

@ -69,7 +69,10 @@ struct GRFError : ZeroedMemoryAllocator {
};
/** Information about GRF, used in the game and (part of it) in savegames */
struct GRFConfig {
struct GRFConfig : ZeroedMemoryAllocator {
GRFConfig(const char *filename = NULL);
~GRFConfig();
GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs
char *filename; ///< Filename - either with or without full path
char *name; ///< NOSAVE: GRF name (Action 0x08)
@ -99,7 +102,6 @@ GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask = 0xFFFFFFFF);
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only);
void AppendStaticGRFConfigs(GRFConfig **dst);
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
void ClearGRFConfig(GRFConfig **config);
void ClearGRFConfigList(GRFConfig **config);
void ResetGRFConfig(bool defaults);
GRFListCompatibility IsGoodGRFConfigList();

@ -44,7 +44,7 @@ static void Load_NGRF()
{
ClearGRFConfigList(&_grfconfig);
while (SlIterateArray() != -1) {
GRFConfig *c = CallocT<GRFConfig>(1);
GRFConfig *c = new GRFConfig();
SlObject(c, _grfconfig_desc);
if (CheckSavegameVersion(101)) c->windows_paletted = (_use_palette == PAL_WINDOWS);
AppendToGRFConfigList(&_grfconfig, c);

@ -1519,9 +1519,8 @@ static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int num)
uint32 grfid = ReadUint32(ls);
if (ReadByte(ls) == 1) {
GRFConfig *c = CallocT<GRFConfig>(1);
GRFConfig *c = new GRFConfig("TTDP game, no information");
c->ident.grfid = grfid;
c->filename = strdup("TTDP game, no information");
AppendToGRFConfigList(&_grfconfig, c);
DEBUG(oldloader, 3, "TTDPatch game using GRF file with GRFID %0X", BSWAP32(c->ident.grfid));

@ -1190,8 +1190,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
if (group == NULL) return NULL;
for (item = group->item; item != NULL; item = item->next) {
GRFConfig *c = CallocT<GRFConfig>(1);
c->filename = strdup(item->name);
GRFConfig *c = new GRFConfig(item->name);
/* Parse parameters */
if (!StrEmpty(item->value)) {
@ -1217,7 +1216,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
}
ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
ClearGRFConfig(&c);
delete c;
continue;
}
@ -1231,7 +1230,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
}
}
if (duplicate) {
ClearGRFConfig(&c);
delete c;
continue;
}

Loading…
Cancel
Save