Use a shared_ptr to avoid having to manage VersionInfo vector memory

develop
Stoyan Gaydarov 2018-07-08 11:34:12 -07:00
parent c127ceab96
commit b5ddde8475
5 changed files with 14 additions and 17 deletions

@ -66,7 +66,7 @@ Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe
// get hash of the running DF process
my_md5 = md5.getHashFromFile(real_path, length, (char *) first_kb);
// create linux process, add it to the vector
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
const VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
if(vinfo)
{
my_descriptor.reset(new VersionInfo(*vinfo));

@ -70,7 +70,7 @@ Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe
// get hash of the running DF process
my_md5 = md5.getHashFromFile(self_exe_name, length, (char *) first_kb);
// create linux process, add it to the vector
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
const VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5);
if(vinfo)
{
my_descriptor.reset(new VersionInfo(*vinfo));

@ -95,7 +95,7 @@ Process::Process(VersionInfoFactory * factory) : identified(false)
return;
}
my_pe = d->pe_header.FileHeader.TimeDateStamp;
VersionInfo* vinfo = factory->getVersionInfoByPETimestamp(my_pe);
const VersionInfo* vinfo = factory->getVersionInfoByPETimestamp(my_pe);
if(vinfo)
{
identified = true;

@ -52,31 +52,26 @@ VersionInfoFactory::~VersionInfoFactory()
void VersionInfoFactory::clear()
{
// for each stored version, delete
for(size_t i = 0; i < versions.size();i++)
{
delete versions[i];
}
versions.clear();
error = false;
}
VersionInfo * VersionInfoFactory::getVersionInfoByMD5(string hash)
const VersionInfo * VersionInfoFactory::getVersionInfoByMD5(string hash)
{
for(size_t i = 0; i < versions.size();i++)
{
if(versions[i]->hasMD5(hash))
return versions[i];
return versions[i].get();
}
return 0;
}
VersionInfo * VersionInfoFactory::getVersionInfoByPETimestamp(uintptr_t timestamp)
const VersionInfo * VersionInfoFactory::getVersionInfoByPETimestamp(uintptr_t timestamp)
{
for(size_t i = 0; i < versions.size();i++)
{
if(versions[i]->hasPE(timestamp))
return versions[i];
return versions[i].get();
}
return 0;
}
@ -230,8 +225,8 @@ bool VersionInfoFactory::loadFile(string path_to_xml)
const char *name = pMemInfo->Attribute("name");
if(name)
{
VersionInfo *version = new VersionInfo();
ParseVersion( pMemInfo , version );
auto version = std::make_shared<VersionInfo>();
ParseVersion( pMemInfo , version.get() );
versions.push_back(version);
}
}

@ -25,6 +25,8 @@ distribution.
#pragma once
#include <memory>
#include "Pragma.h"
#include "Export.h"
@ -39,9 +41,9 @@ namespace DFHack
~VersionInfoFactory();
bool loadFile( std::string path_to_xml);
bool isInErrorState() const {return error;};
VersionInfo * getVersionInfoByMD5(std::string md5string);
VersionInfo * getVersionInfoByPETimestamp(uintptr_t timestamp);
std::vector<VersionInfo*> versions;
const VersionInfo * getVersionInfoByMD5(std::string md5string);
const VersionInfo * getVersionInfoByPETimestamp(uintptr_t timestamp);
std::vector<std::shared_ptr<const VersionInfo>> versions;
// trash existing list
void clear();
private: