CResource: Make BuildDependencyTree() return a unique_ptr

Makes the functions more memory safe in terms of freeing memory in
exceptional paths .
This commit is contained in:
Lioncash
2020-06-11 18:25:35 -04:00
parent eb8ca98a8a
commit ce315280c3
24 changed files with 82 additions and 93 deletions

View File

@@ -92,7 +92,6 @@ CResourceEntry* CResourceEntry::BuildFromDirectory(CResourceStore *pStore, CResT
CResourceEntry::~CResourceEntry()
{
if (mpResource) delete mpResource;
if (mpDependencies) delete mpDependencies;
}
bool CResourceEntry::LoadMetadata()
@@ -171,15 +170,11 @@ void CResourceEntry::SerializeEntryInfo(IArchive& rArc, bool MetadataOnly)
void CResourceEntry::UpdateDependencies()
{
if (mpDependencies)
{
delete mpDependencies;
mpDependencies = nullptr;
}
mpDependencies.reset();
if (!mpTypeInfo->CanHaveDependencies())
{
mpDependencies = new CDependencyTree();
mpDependencies = std::make_unique<CDependencyTree>();
return;
}
@@ -191,7 +186,7 @@ void CResourceEntry::UpdateDependencies()
if (!mpResource)
{
errorf("Unable to update cached dependencies; failed to load resource");
mpDependencies = new CDependencyTree();
mpDependencies = std::make_unique<CDependencyTree>();
return;
}

View File

@@ -30,7 +30,7 @@ class CResourceEntry
CResource *mpResource;
CResTypeInfo *mpTypeInfo;
CResourceStore *mpStore;
CDependencyTree *mpDependencies;
std::unique_ptr<CDependencyTree> mpDependencies;
CAssetID mID;
CVirtualDirectory *mpDirectory;
TString mName;
@@ -100,7 +100,7 @@ public:
CResource* Resource() const { return mpResource; }
CResTypeInfo* TypeInfo() const { return mpTypeInfo; }
CResourceStore* ResourceStore() const { return mpStore; }
CDependencyTree* Dependencies() const { return mpDependencies; }
CDependencyTree* Dependencies() const { return mpDependencies.get(); }
CAssetID ID() const { return mID; }
CVirtualDirectory* Directory() const { return mpDirectory; }
TString DirectoryPath() const { return mpDirectory->FullPath(); }