From 2a5ab1ed327293090a6170c50782420a7d28b70a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 11 Jun 2020 21:05:14 -0400 Subject: [PATCH] CAnimEventLoader: Use unique_ptr more Makes memory handling a little more robust. --- src/Core/Resource/Animation/CAnimSet.h | 7 ++++--- src/Core/Resource/Factory/CAnimEventLoader.cpp | 17 +++++++++++------ src/Core/Resource/Factory/CAnimEventLoader.h | 4 ++-- src/Core/Resource/Factory/CAnimSetLoader.cpp | 6 ++---- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Core/Resource/Animation/CAnimSet.h b/src/Core/Resource/Animation/CAnimSet.h index 560f5281..4c758b23 100644 --- a/src/Core/Resource/Animation/CAnimSet.h +++ b/src/Core/Resource/Animation/CAnimSet.h @@ -14,6 +14,8 @@ #include "Core/Resource/Model/CModel.h" #include +#include +#include #include // Animation structures @@ -99,7 +101,7 @@ class CAnimSet : public CResource float mDefaultAdditiveFadeIn; float mDefaultAdditiveFadeOut; std::vector mHalfTransitions; - std::vector mAnimEvents; // note: these are for MP2, where event data isn't a standalone resource; these are owned by the animset + std::vector> mAnimEvents; // note: these are for MP2, where event data isn't a standalone resource; these are owned by the animset public: CAnimSet(CResourceEntry *pEntry = 0) @@ -124,7 +126,6 @@ public: for (uint32 iEvent = 0; iEvent < mAnimEvents.size(); iEvent++) { ASSERT(mAnimEvents[iEvent] && !mAnimEvents[iEvent]->Entry()); - delete mAnimEvents[iEvent]; } } @@ -232,7 +233,7 @@ public: else { - return (Index < mAnimEvents.size() ? mAnimEvents[Index] : nullptr); + return (Index < mAnimEvents.size() ? mAnimEvents[Index].get() : nullptr); } } }; diff --git a/src/Core/Resource/Factory/CAnimEventLoader.cpp b/src/Core/Resource/Factory/CAnimEventLoader.cpp index ed76fe41..66c7403e 100644 --- a/src/Core/Resource/Factory/CAnimEventLoader.cpp +++ b/src/Core/Resource/Factory/CAnimEventLoader.cpp @@ -142,19 +142,24 @@ std::unique_ptr CAnimEventLoader::LoadEVNT(IInputStream& rEVNT, return ptr; } -CAnimEventData* CAnimEventLoader::LoadAnimSetEvents(IInputStream& rANCS) +std::unique_ptr CAnimEventLoader::LoadAnimSetEvents(IInputStream& rANCS) { + auto ptr = std::make_unique(); + CAnimEventLoader Loader; - Loader.mpEventData = new CAnimEventData(); + Loader.mpEventData = ptr.get(); Loader.mGame = EGame::Echoes; Loader.LoadEvents(rANCS); - return Loader.mpEventData; + + return ptr; } -CAnimEventData* CAnimEventLoader::LoadCorruptionCharacterEventSet(IInputStream& rCHAR) +std::unique_ptr CAnimEventLoader::LoadCorruptionCharacterEventSet(IInputStream& rCHAR) { + auto ptr = std::make_unique(); + CAnimEventLoader Loader; - Loader.mpEventData = new CAnimEventData(); + Loader.mpEventData = ptr.get(); Loader.mGame = EGame::Corruption; // Read event set header @@ -179,5 +184,5 @@ CAnimEventData* CAnimEventLoader::LoadCorruptionCharacterEventSet(IInputStream& Loader.LoadSoundEvent(rCHAR); } - return Loader.mpEventData; + return ptr; } diff --git a/src/Core/Resource/Factory/CAnimEventLoader.h b/src/Core/Resource/Factory/CAnimEventLoader.h index 469a83da..4af32a01 100644 --- a/src/Core/Resource/Factory/CAnimEventLoader.h +++ b/src/Core/Resource/Factory/CAnimEventLoader.h @@ -20,8 +20,8 @@ class CAnimEventLoader public: static std::unique_ptr LoadEVNT(IInputStream& rEVNT, CResourceEntry *pEntry); - static CAnimEventData* LoadAnimSetEvents(IInputStream& rANCS); - static CAnimEventData* LoadCorruptionCharacterEventSet(IInputStream& rCHAR); + static std::unique_ptr LoadAnimSetEvents(IInputStream& rANCS); + static std::unique_ptr LoadCorruptionCharacterEventSet(IInputStream& rCHAR); }; #endif // CANIMEVENTLOADER_H diff --git a/src/Core/Resource/Factory/CAnimSetLoader.cpp b/src/Core/Resource/Factory/CAnimSetLoader.cpp index 81d4b503..1de50e47 100644 --- a/src/Core/Resource/Factory/CAnimSetLoader.cpp +++ b/src/Core/Resource/Factory/CAnimSetLoader.cpp @@ -41,8 +41,7 @@ void CAnimSetLoader::LoadCorruptionCHAR(IInputStream& rCHAR) for (uint32 iSet = 0; iSet < NumEventSets; iSet++) { - CAnimEventData *pEvents = CAnimEventLoader::LoadCorruptionCharacterEventSet(rCHAR); - pSet->mAnimEvents.push_back(pEvents); + pSet->mAnimEvents.push_back(CAnimEventLoader::LoadCorruptionCharacterEventSet(rCHAR)); } // Animations @@ -383,8 +382,7 @@ void CAnimSetLoader::LoadAnimationSet(IInputStream& rANCS) for (uint32 iEvent = 0; iEvent < EventDataCount; iEvent++) { - CAnimEventData *pData = CAnimEventLoader::LoadAnimSetEvents(rANCS); - pSet->mAnimEvents.push_back(pData); + pSet->mAnimEvents.push_back(CAnimEventLoader::LoadAnimSetEvents(rANCS)); } } }