diff --git a/src/g_game.cpp b/src/g_game.cpp index 77cc53a76..0d22065a7 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -1840,7 +1840,7 @@ void G_DoLoadGame () SaveVersion = 0; - void *data = info->CacheLump(); + void *data = info->Lock(); FSerializer arc(nullptr); if (!arc.OpenReader((const char *)data, info->LumpSize)) { @@ -1916,7 +1916,7 @@ void G_DoLoadGame () return; } - data = info->CacheLump(); + data = info->Lock(); if (!arc.OpenReader((const char *)data, info->LumpSize)) { LoadGameError("TXT_SGINFOERR"); diff --git a/src/gamedata/resourcefiles/resourcefile.cpp b/src/gamedata/resourcefiles/resourcefile.cpp index ae136e9b6..347c25388 100644 --- a/src/gamedata/resourcefiles/resourcefile.cpp +++ b/src/gamedata/resourcefiles/resourcefile.cpp @@ -58,13 +58,13 @@ public: FLumpReader(FResourceLump *src) : MemoryReader(NULL, src->LumpSize), source(src) { - src->CacheLump(); + src->Lock(); bufptr = src->Cache; } ~FLumpReader() { - source->ReleaseCache(); + source->Unlock(); } }; @@ -163,9 +163,9 @@ void FResourceLump::CheckEmbedded() FCompressedBuffer FResourceLump::GetRawData() { FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)LumpSize, METHOD_STORED, 0, 0, new char[LumpSize] }; - memcpy(cbuf.mBuffer, CacheLump(), LumpSize); + memcpy(cbuf.mBuffer, Lock(), LumpSize); + Unlock(); cbuf.mCRC32 = crc32(0, (uint8_t*)cbuf.mBuffer, LumpSize); - ReleaseCache(); return cbuf; } @@ -198,7 +198,7 @@ FileReader FResourceLump::NewReader() // //========================================================================== -void *FResourceLump::CacheLump() +void *FResourceLump::Lock() { if (Cache != NULL) { @@ -217,7 +217,7 @@ void *FResourceLump::CacheLump() // //========================================================================== -int FResourceLump::ReleaseCache() +int FResourceLump::Unlock() { if (LumpSize > 0 && RefCount > 0) { diff --git a/src/gamedata/resourcefiles/resourcefile.h b/src/gamedata/resourcefiles/resourcefile.h index d0a0e40c3..fcf244040 100644 --- a/src/gamedata/resourcefiles/resourcefile.h +++ b/src/gamedata/resourcefiles/resourcefile.h @@ -102,9 +102,11 @@ public: void CheckEmbedded(); virtual FCompressedBuffer GetRawData(); - void *CacheLump(); - int ReleaseCache(); + void *Lock(); // validates the cache and increases the refcount. + int Unlock(); // decreases the refcount and frees the buffer + unsigned Size() const{ return LumpSize; } + int LockCount() const { return RefCount; } const char* getName() { return FullName.GetChars(); } protected: diff --git a/src/menu/loadsavemenu.cpp b/src/menu/loadsavemenu.cpp index 86a7dafbb..ac14bd985 100644 --- a/src/menu/loadsavemenu.cpp +++ b/src/menu/loadsavemenu.cpp @@ -170,7 +170,7 @@ void FSavegameManager::ReadSaveStrings() // I_FindName only returns the file's name and not its full path FString filepath = G_BuildSaveName(I_FindName(&c_file), -1); - FResourceFile *savegame = FResourceFile::OpenResourceFile(filepath, true, true); + std::unique_ptr savegame(FResourceFile::OpenResourceFile(filepath, true, true)); if (savegame != nullptr) { bool oldVer = false; @@ -179,10 +179,9 @@ void FSavegameManager::ReadSaveStrings() if (info == nullptr) { // savegame info not found. This is not a savegame so leave it alone. - delete savegame; continue; } - void *data = info->CacheLump(); + void *data = info->Lock(); FSerializer arc(nullptr); if (arc.OpenReader((const char *)data, info->LumpSize)) { @@ -197,7 +196,6 @@ void FSavegameManager::ReadSaveStrings() { // different engine or newer version: // not our business. Leave it alone. - delete savegame; continue; } @@ -213,7 +211,6 @@ void FSavegameManager::ReadSaveStrings() else { // different game. Skip this. - delete savegame; continue; } @@ -223,7 +220,6 @@ void FSavegameManager::ReadSaveStrings() node->bMissingWads = missing; node->SaveTitle = title; InsertSaveNode(node); - delete savegame; } } @@ -472,7 +468,7 @@ unsigned FSavegameManager::ExtractSaveData(int index) // this should not happen because the file has already been verified. return index; } - void *data = info->CacheLump(); + void *data = info->Lock(); FSerializer arc(nullptr); if (arc.OpenReader((const char *)data, info->LumpSize)) { @@ -494,10 +490,9 @@ unsigned FSavegameManager::ExtractSaveData(int index) picreader.OpenMemoryArray([=](TArray &array) { - auto cache = pic->CacheLump(); + auto cache = pic->Lock(); array.Resize(pic->LumpSize); memcpy(&array[0], cache, pic->LumpSize); - pic->ReleaseCache(); return true; }); PNGHandle *png = M_VerifyPNG(picreader);