- use std::unique_ptr to manage the resource file for loading a savegame because the try/catch handler to ensure its deletion was causing some problems.

This commit is contained in:
Christoph Oelckers 2017-01-22 20:04:38 +01:00
parent f9ef935840
commit f15b051327

View file

@ -1891,18 +1891,15 @@ void G_DoLoadGame ()
hidecon = gameaction == ga_loadgamehidecon;
gameaction = ga_nothing;
FResourceFile *resfile = FResourceFile::OpenResourceFile(savename.GetChars(), nullptr, true, true);
std::unique_ptr<FResourceFile> resfile(FResourceFile::OpenResourceFile(savename.GetChars(), nullptr, true, true));
if (resfile == nullptr)
{
Printf ("Could not read savegame '%s'\n", savename.GetChars());
return;
}
try
{
FResourceLump *info = resfile->FindLump("info.json");
if (info == nullptr)
{
delete resfile;
Printf("'%s' is not a valid savegame: Missing 'info.json'.\n", savename.GetChars());
return;
}
@ -1914,7 +1911,6 @@ void G_DoLoadGame ()
if (!arc.OpenReader((const char *)data, info->LumpSize))
{
Printf("Failed to access savegame info\n");
delete resfile;
return;
}
@ -1938,13 +1934,11 @@ void G_DoLoadGame ()
{
Printf("Savegame is from another ZDoom-based engine: %s\n", engine.GetChars());
}
delete resfile;
return;
}
if (SaveVersion < MINSAVEVER || SaveVersion > SAVEVER)
{
delete resfile;
Printf("Savegame is from an incompatible version");
if (SaveVersion < MINSAVEVER)
{
@ -1960,14 +1954,12 @@ void G_DoLoadGame ()
if (!G_CheckSaveGameWads(arc, true))
{
delete resfile;
return;
}
if (map.IsEmpty())
{
Printf("Savegame is missing the current map\n");
delete resfile;
return;
}
@ -1983,7 +1975,6 @@ void G_DoLoadGame ()
info = resfile->FindLump("globals.json");
if (info == nullptr)
{
delete resfile;
Printf("'%s' is not a valid savegame: Missing 'globals.json'.\n", savename.GetChars());
return;
}
@ -1992,7 +1983,6 @@ void G_DoLoadGame ()
if (!arc.OpenReader((const char *)data, info->LumpSize))
{
Printf("Failed to access savegame info\n");
delete resfile;
return;
}
@ -2017,9 +2007,8 @@ void G_DoLoadGame ()
// dearchive all the modifications
level.time = Scale(time[1], TICRATE, time[0]);
G_ReadSnapshots(resfile);
delete resfile; // we no longer need the resource file below this point
resfile = nullptr;
G_ReadSnapshots(resfile.get());
resfile.reset(nullptr); // we no longer need the resource file below this point
G_ReadVisited(arc);
// load a base level
@ -2046,13 +2035,6 @@ void G_DoLoadGame ()
// amount of memory in use, so bring it down now by starting a
// collection.
GC::StartCollection();
}
catch (...)
{
// delete the resource file if anything goes wrong in here.
if (resfile != nullptr) delete resfile;
throw;
}
}