From 0a59489824b8496dfd9f772a90a9703cbcc6eeeb Mon Sep 17 00:00:00 2001 From: wraitii Date: Tue, 13 Apr 2021 13:46:45 +0000 Subject: [PATCH] Use unique_ptr for secondary sim data. Differential Revision: https://code.wildfiregames.com/D3842 This was SVN commit r25250. --- source/simulation2/Simulation2.cpp | 39 ++++++++++++------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 4dc509334f..c37b0df599 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -46,6 +46,7 @@ #include #include +#include class CSimulation2Impl { @@ -53,7 +54,6 @@ public: CSimulation2Impl(CUnitManager* unitManager, shared_ptr cx, CTerrain* terrain) : m_SimContext(), m_ComponentManager(m_SimContext, cx), m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), - m_SecondaryTerrain(nullptr), m_SecondaryContext(nullptr), m_SecondaryComponentManager(nullptr), m_SecondaryLoadedScripts(nullptr), m_MapSettings(cx->GetGeneralJSContext()), m_InitAttributes(cx->GetGeneralJSContext()) { m_SimContext.m_UnitManager = unitManager; @@ -81,11 +81,6 @@ public: ~CSimulation2Impl() { - delete m_SecondaryTerrain; - delete m_SecondaryContext; - delete m_SecondaryComponentManager; - delete m_SecondaryLoadedScripts; - UnregisterFileReloadFunc(ReloadChangedFileCB, this); } @@ -143,11 +138,11 @@ public: int m_RejoinTestTurn; bool m_TestingRejoin; - // Secondary simulation - CTerrain* m_SecondaryTerrain; - CSimContext* m_SecondaryContext; - CComponentManager* m_SecondaryComponentManager; - std::set* m_SecondaryLoadedScripts; + // Secondary simulation (NB: order matters for destruction). + std::unique_ptr m_SecondaryComponentManager; + std::unique_ptr m_SecondaryTerrain; + std::unique_ptr m_SecondaryContext; + std::unique_ptr> m_SecondaryLoadedScripts; struct SerializationTestState { @@ -404,20 +399,16 @@ void CSimulation2Impl::Update(int turnLength, const std::vector(); - delete m_SecondaryContext; - m_SecondaryContext = new CSimContext(); - m_SecondaryContext->m_Terrain = m_SecondaryTerrain; + m_SecondaryContext = std::make_unique(); + m_SecondaryContext->m_Terrain = m_SecondaryTerrain.get(); - delete m_SecondaryComponentManager; - m_SecondaryComponentManager = new CComponentManager(*m_SecondaryContext, scriptInterface.GetContext()); + m_SecondaryComponentManager = std::make_unique(*m_SecondaryContext, scriptInterface.GetContext()); m_SecondaryComponentManager->LoadComponentTypes(); - delete m_SecondaryLoadedScripts; - m_SecondaryLoadedScripts = new std::set(); - ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts)); + m_SecondaryLoadedScripts = std::make_unique>(); + ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts.get())); ResetComponentState(*m_SecondaryComponentManager, false, false); // Load the trigger scripts after we have loaded the simulation. @@ -425,7 +416,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vectorGetScriptInterface()); JS::RootedValue mapSettingsCloned(rq2.cx, m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherCompartment(scriptInterface, m_MapSettings)); - ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); + ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts.get())); } // Load the map into the secondary simulation @@ -447,8 +438,8 @@ void CSimulation2Impl::Update(int turnLength, const std::vectorLoadMap(mapfilename, *scriptInterface.GetContext(), JS::UndefinedHandleValue, - m_SecondaryTerrain, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, m_SecondaryContext, INVALID_PLAYER, true); // throws exception on failure + m_SecondaryTerrain.get(), NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, m_SecondaryContext.get(), INVALID_PLAYER, true); // throws exception on failure } LDR_EndRegistering();