From c127ceab964ac75745613dd24d0675ac9d20c825 Mon Sep 17 00:00:00 2001 From: Stoyan Gaydarov Date: Sun, 8 Jul 2018 11:19:19 -0700 Subject: [PATCH] Use a unique_ptr for VersionInfo to avoid worrying about memory --- library/Process-darwin.cpp | 11 +++-------- library/Process-linux.cpp | 11 +++-------- library/Process-windows.cpp | 7 ++----- library/include/MemAccess.h | 5 +++-- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/library/Process-darwin.cpp b/library/Process-darwin.cpp index 2091d9a96..58e78d7ce 100644 --- a/library/Process-darwin.cpp +++ b/library/Process-darwin.cpp @@ -48,7 +48,7 @@ using namespace std; #include using namespace DFHack; -Process::Process(VersionInfoFactory * known_versions) +Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe(0) { int target_result; @@ -59,10 +59,6 @@ Process::Process(VersionInfoFactory * known_versions) real_path = realpath(path, NULL); } - identified = false; - my_descriptor = 0; - my_pe = 0; - md5wrapper md5; uint32_t length; uint8_t first_kb [1024]; @@ -73,7 +69,7 @@ Process::Process(VersionInfoFactory * known_versions) VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); if(vinfo) { - my_descriptor = new VersionInfo(*vinfo); + my_descriptor.reset(new VersionInfo(*vinfo)); identified = true; } else @@ -112,8 +108,7 @@ Process::Process(VersionInfoFactory * known_versions) Process::~Process() { - // destroy our copy of the memory descriptor - delete my_descriptor; + // Nothing to do here } string Process::doReadClassName (void * vptr) diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index fa06d28e4..f18eff5f8 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -46,7 +46,7 @@ using namespace std; #include using namespace DFHack; -Process::Process(VersionInfoFactory * known_versions) +Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe(0) { const char * dir_name = "/proc/self/"; const char * exe_link_name = "/proc/self/exe"; @@ -54,10 +54,6 @@ Process::Process(VersionInfoFactory * known_versions) const char * cmdline_name = "/proc/self/cmdline"; int target_result; - identified = false; - my_descriptor = 0; - my_pe = 0; - // valgrind replaces readlink for /proc/self/exe, but not open. char self_exe[1024]; memset(self_exe, 0, sizeof(self_exe)); @@ -77,7 +73,7 @@ Process::Process(VersionInfoFactory * known_versions) VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); if(vinfo) { - my_descriptor = new VersionInfo(*vinfo); + my_descriptor.reset(new VersionInfo(*vinfo)); identified = true; } else @@ -116,8 +112,7 @@ Process::Process(VersionInfoFactory * known_versions) Process::~Process() { - // destroy our copy of the memory descriptor - delete my_descriptor; + // Nothing to do here } string Process::doReadClassName (void * vptr) diff --git a/library/Process-windows.cpp b/library/Process-windows.cpp index 3a1da0ac4..aa20ec8b8 100644 --- a/library/Process-windows.cpp +++ b/library/Process-windows.cpp @@ -62,13 +62,11 @@ namespace DFHack char * base; }; } -Process::Process(VersionInfoFactory * factory) +Process::Process(VersionInfoFactory * factory) : identified(false) { HMODULE hmod = NULL; DWORD needed; bool found = false; - identified = false; - my_descriptor = NULL; d = new PlatformSpecific(); // open process @@ -102,7 +100,7 @@ Process::Process(VersionInfoFactory * factory) { identified = true; // give the process a data model and memory layout fixed for the base of first module - my_descriptor = new VersionInfo(*vinfo); + my_descriptor.reset(new VersionInfo(*vinfo)); my_descriptor->rebaseTo(getBase()); } else @@ -115,7 +113,6 @@ Process::Process(VersionInfoFactory * factory) Process::~Process() { // destroy our rebased copy of the memory descriptor - delete my_descriptor; if(d->sections != NULL) free(d->sections); } diff --git a/library/include/MemAccess.h b/library/include/MemAccess.h index 3022688f9..da94e81df 100644 --- a/library/include/MemAccess.h +++ b/library/include/MemAccess.h @@ -33,6 +33,7 @@ distribution. #include #include #include +#include namespace DFHack { @@ -248,7 +249,7 @@ namespace DFHack /// get the symbol table extension of this process VersionInfo *getDescriptor() { - return my_descriptor; + return my_descriptor.get(); }; uintptr_t getBase(); /// get the DF Process ID @@ -291,7 +292,7 @@ namespace DFHack std::string getMD5() { return my_md5; } private: - VersionInfo * my_descriptor; + std::unique_ptr my_descriptor; PlatformSpecific *d; bool identified; uint32_t my_pid;