Use a unique_ptr for VersionInfo to avoid worrying about memory

develop
Stoyan Gaydarov 2018-07-08 11:19:19 -07:00
parent 44b7e8df71
commit c127ceab96
4 changed files with 11 additions and 23 deletions

@ -48,7 +48,7 @@ using namespace std;
#include <string.h> #include <string.h>
using namespace DFHack; using namespace DFHack;
Process::Process(VersionInfoFactory * known_versions) Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe(0)
{ {
int target_result; int target_result;
@ -59,10 +59,6 @@ Process::Process(VersionInfoFactory * known_versions)
real_path = realpath(path, NULL); real_path = realpath(path, NULL);
} }
identified = false;
my_descriptor = 0;
my_pe = 0;
md5wrapper md5; md5wrapper md5;
uint32_t length; uint32_t length;
uint8_t first_kb [1024]; uint8_t first_kb [1024];
@ -73,7 +69,7 @@ Process::Process(VersionInfoFactory * known_versions)
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
if(vinfo) if(vinfo)
{ {
my_descriptor = new VersionInfo(*vinfo); my_descriptor.reset(new VersionInfo(*vinfo));
identified = true; identified = true;
} }
else else
@ -112,8 +108,7 @@ Process::Process(VersionInfoFactory * known_versions)
Process::~Process() Process::~Process()
{ {
// destroy our copy of the memory descriptor // Nothing to do here
delete my_descriptor;
} }
string Process::doReadClassName (void * vptr) string Process::doReadClassName (void * vptr)

@ -46,7 +46,7 @@ using namespace std;
#include <string.h> #include <string.h>
using namespace DFHack; 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 * dir_name = "/proc/self/";
const char * exe_link_name = "/proc/self/exe"; const char * exe_link_name = "/proc/self/exe";
@ -54,10 +54,6 @@ Process::Process(VersionInfoFactory * known_versions)
const char * cmdline_name = "/proc/self/cmdline"; const char * cmdline_name = "/proc/self/cmdline";
int target_result; int target_result;
identified = false;
my_descriptor = 0;
my_pe = 0;
// valgrind replaces readlink for /proc/self/exe, but not open. // valgrind replaces readlink for /proc/self/exe, but not open.
char self_exe[1024]; char self_exe[1024];
memset(self_exe, 0, sizeof(self_exe)); memset(self_exe, 0, sizeof(self_exe));
@ -77,7 +73,7 @@ Process::Process(VersionInfoFactory * known_versions)
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
if(vinfo) if(vinfo)
{ {
my_descriptor = new VersionInfo(*vinfo); my_descriptor.reset(new VersionInfo(*vinfo));
identified = true; identified = true;
} }
else else
@ -116,8 +112,7 @@ Process::Process(VersionInfoFactory * known_versions)
Process::~Process() Process::~Process()
{ {
// destroy our copy of the memory descriptor // Nothing to do here
delete my_descriptor;
} }
string Process::doReadClassName (void * vptr) string Process::doReadClassName (void * vptr)

@ -62,13 +62,11 @@ namespace DFHack
char * base; char * base;
}; };
} }
Process::Process(VersionInfoFactory * factory) Process::Process(VersionInfoFactory * factory) : identified(false)
{ {
HMODULE hmod = NULL; HMODULE hmod = NULL;
DWORD needed; DWORD needed;
bool found = false; bool found = false;
identified = false;
my_descriptor = NULL;
d = new PlatformSpecific(); d = new PlatformSpecific();
// open process // open process
@ -102,7 +100,7 @@ Process::Process(VersionInfoFactory * factory)
{ {
identified = true; identified = true;
// give the process a data model and memory layout fixed for the base of first module // 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()); my_descriptor->rebaseTo(getBase());
} }
else else
@ -115,7 +113,6 @@ Process::Process(VersionInfoFactory * factory)
Process::~Process() Process::~Process()
{ {
// destroy our rebased copy of the memory descriptor // destroy our rebased copy of the memory descriptor
delete my_descriptor;
if(d->sections != NULL) if(d->sections != NULL)
free(d->sections); free(d->sections);
} }

@ -33,6 +33,7 @@ distribution.
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <map> #include <map>
#include <memory>
namespace DFHack namespace DFHack
{ {
@ -248,7 +249,7 @@ namespace DFHack
/// get the symbol table extension of this process /// get the symbol table extension of this process
VersionInfo *getDescriptor() VersionInfo *getDescriptor()
{ {
return my_descriptor; return my_descriptor.get();
}; };
uintptr_t getBase(); uintptr_t getBase();
/// get the DF Process ID /// get the DF Process ID
@ -291,7 +292,7 @@ namespace DFHack
std::string getMD5() { return my_md5; } std::string getMD5() { return my_md5; }
private: private:
VersionInfo * my_descriptor; std::unique_ptr<VersionInfo> my_descriptor;
PlatformSpecific *d; PlatformSpecific *d;
bool identified; bool identified;
uint32_t my_pid; uint32_t my_pid;