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>
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)

@ -46,7 +46,7 @@ using namespace std;
#include <string.h>
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)

@ -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);
}

@ -33,6 +33,7 @@ distribution.
#include <iostream>
#include <cstring>
#include <map>
#include <memory>
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<VersionInfo> my_descriptor;
PlatformSpecific *d;
bool identified;
uint32_t my_pid;