From 2cc79fe9c800c2f6e711d135e2039461af7e199e Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Sat, 18 Nov 2017 11:50:40 -0600 Subject: FULLPIPE: Fix memory leaks restoring save games --- engines/fullpipe/stateloader.cpp | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/engines/fullpipe/stateloader.cpp b/engines/fullpipe/stateloader.cpp index 4041eb15c9..231379cddc 100644 --- a/engines/fullpipe/stateloader.cpp +++ b/engines/fullpipe/stateloader.cpp @@ -42,7 +42,7 @@ namespace Fullpipe { bool GameLoader::readSavegame(const char *fname) { SaveHeader header; - Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading(fname); + Common::ScopedPtr saveFile(g_system->getSavefileManager()->openForLoading(fname)); if (!saveFile) { warning("Cannot open save %s for loading", fname); @@ -63,29 +63,28 @@ bool GameLoader::readSavegame(const char *fname) { _updateCounter = header.updateCounter; - byte *data = (byte *)malloc(header.encSize); - saveFile->read(data, header.encSize); + Common::Array data(header.encSize); + saveFile->read(data.data(), header.encSize); - byte *map = (byte *)malloc(800); - saveFile->read(map, 800); + Common::Array map(800); + saveFile->read(map.data(), 800); - Common::MemoryReadStream *tempStream = new Common::MemoryReadStream(map, 800); - MfcArchive temp(tempStream); + { + Common::MemoryReadStream tempStream(map.data(), 800, DisposeAfterUse::NO); + MfcArchive temp(&tempStream); - if (_savegameCallback) - _savegameCallback(&temp, false); - - delete tempStream; - delete saveFile; + if (_savegameCallback) + _savegameCallback(&temp, false); + } // Deobfuscate the data for (int i = 0; i < header.encSize; i++) data[i] -= i & 0x7f; - Common::MemoryReadStream *archiveStream = new Common::MemoryReadStream(data, header.encSize); - MfcArchive *archive = new MfcArchive(archiveStream); + Common::MemoryReadStream archiveStream(data.data(), header.encSize, DisposeAfterUse::NO); + MfcArchive archive(&archiveStream); - GameVar *var = archive->readClass(); + GameVar *var = archive.readClass(); GameVar *v = _gameVar->getSubVarByName("OBJSTATES"); @@ -94,23 +93,22 @@ bool GameLoader::readSavegame(const char *fname) { if (!v) { warning("No state to save"); - delete archiveStream; - delete archive; + delete var; return false; } } addVar(var, v); - getGameLoaderInventory()->loadPartial(*archive); + getGameLoaderInventory()->loadPartial(archive); - uint32 arrSize = archive->readUint32LE(); + uint32 arrSize = archive.readUint32LE(); debugC(3, kDebugLoading, "Reading %d infos", arrSize); for (uint i = 0; i < arrSize; i++) { - const uint picAniInfosCount = archive->readUint32LE(); + const uint picAniInfosCount = archive.readUint32LE(); if (picAniInfosCount) debugC(3, kDebugLoading, "Count %d: %d", i, picAniInfosCount); @@ -118,15 +116,12 @@ bool GameLoader::readSavegame(const char *fname) { _sc2array[i]._picAniInfos.resize(picAniInfosCount); for (uint j = 0; j < picAniInfosCount; j++) { - _sc2array[i]._picAniInfos[j].load(*archive); + _sc2array[i]._picAniInfos[j].load(archive); } _sc2array[i]._isLoaded = false; } - delete archiveStream; - delete archive; - getGameLoaderInventory()->rebuildItemRects(); PreloadItem preloadItem; -- cgit v1.2.3