From 12c8046f90dd59ef7f5f54ec79bb856b1eb7d2c5 Mon Sep 17 00:00:00 2001 From: Stoyan Gaydarov Date: Sun, 8 Jul 2018 12:04:53 -0700 Subject: [PATCH] Some memory management changes for Core --- library/Core.cpp | 16 +++++++--------- library/LuaApi.cpp | 4 ++-- library/include/Core.h | 5 +++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 69ef5a132..d4db9bb08 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1518,7 +1518,7 @@ Core::~Core() } Core::Core() : - d{new Private}, + d(new Private), script_path_mutex{}, HotkeyMutex{}, HotkeyCond{}, @@ -1630,10 +1630,10 @@ bool Core::Init() fatal(out.str()); return false; } - p = new DFHack::Process(vif); - vinfo = p->getDescriptor(); + std::unique_ptr local_p(new DFHack::Process(vif)); + vinfo = local_p->getDescriptor(); - if(!vinfo || !p->isIdentified()) + if(!vinfo || !local_p->isIdentified()) { if (!Version::git_xml_match()) { @@ -1664,11 +1664,10 @@ bool Core::Init() fatal("Not a known DF version.\n"); } errorstate = true; - delete p; - p = NULL; return false; } cerr << "Version: " << vinfo->getVersion() << endl; + p = std::move(local_p); #if defined(_WIN32) const OSType expected = OS_WINDOWS; @@ -2321,8 +2320,7 @@ int Core::Shutdown ( void ) } allModules.clear(); memset(&(s_mods), 0, sizeof(s_mods)); - delete d; - d = nullptr; + d.reset(); return -1; } @@ -2751,7 +2749,7 @@ void ClassNameCheck::getKnownClassNames(std::vector &names) MemoryPatcher::MemoryPatcher(Process *p_) : p(p_) { if (!p) - p = Core::getInstance().p; + p = Core::getInstance().p.get(); } MemoryPatcher::~MemoryPatcher() diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index a435c7a8e..2840aa5b9 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2521,7 +2521,7 @@ static const LuaWrapper::FunctionReg dfhack_internal_module[] = { static int internal_getmd5(lua_State *L) { - auto p = Core::getInstance().p; + auto& p = Core::getInstance().p; if (p->getDescriptor()->getOS() == OS_WINDOWS) luaL_error(L, "process MD5 not available on Windows"); lua_pushstring(L, p->getMD5().c_str()); @@ -2530,7 +2530,7 @@ static int internal_getmd5(lua_State *L) static int internal_getPE(lua_State *L) { - auto p = Core::getInstance().p; + auto& p = Core::getInstance().p; if (p->getDescriptor()->getOS() != OS_WINDOWS) luaL_error(L, "process PE timestamp not available on non-Windows"); lua_pushinteger(L, p->getPE()); diff --git a/library/include/Core.h b/library/include/Core.h index 529633ff2..9ea24f15a 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -30,6 +30,7 @@ distribution. #include #include #include +#include #include #include "Console.h" #include "modules/Graphic.h" @@ -191,7 +192,7 @@ namespace DFHack DFHack::Console &getConsole() { return con; } - DFHack::Process * p; + std::unique_ptr p; DFHack::VersionInfo * vinfo; DFHack::Windows::df_window * screen_window; @@ -209,7 +210,7 @@ namespace DFHack ~Core(); struct Private; - Private *d; + std::unique_ptr d; bool Init(); int Update (void);