Index: source/graphics/MapReader.h =================================================================== --- source/graphics/MapReader.h +++ source/graphics/MapReader.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2016 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -86,9 +86,6 @@ // read entity data from the XML file int ReadXMLEntities(); - // clean up everything used during delayed load - int DelayLoadFinished(); - // Copy random map settings over to sim int LoadRMSettings(); @@ -128,7 +125,6 @@ CMapGenerator* m_MapGen; - // state latched by LoadMap and held until DelayedLoadFinished CFileUnpacker unpacker; CTerrain* pTerrain; WaterManager* pWaterMan; Index: source/graphics/MapReader.cpp =================================================================== --- source/graphics/MapReader.cpp +++ source/graphics/MapReader.cpp @@ -70,7 +70,6 @@ CLightEnv *pLightEnv_, CGameView *pGameView_, CCinemaManager* pCinema_, CTriggerManager* pTrigMan_, CPostprocManager* pPostproc_, CSimulation2 *pSimulation2_, const CSimContext* pSimContext_, int playerID_, bool skipEntities) { - // latch parameters (held until DelayedLoadFinished) pTerrain = pTerrain_; pLightEnv = pLightEnv_; pGameView = pGameView_; @@ -145,8 +144,6 @@ // load map settings script (must be done after reading map) RegMemFun(this, &CMapReader::LoadMapSettings, L"CMapReader::LoadMapSettings", 5); - - RegMemFun(this, &CMapReader::DelayLoadFinished, L"CMapReader::DelayLoadFinished", 5); } // LoadRandomMap: try to load the map data; reinitialise the scene to new data if successful @@ -155,7 +152,6 @@ CLightEnv *pLightEnv_, CGameView *pGameView_, CCinemaManager* pCinema_, CTriggerManager* pTrigMan_, CPostprocManager* pPostproc_, CSimulation2 *pSimulation2_, int playerID_) { - // latch parameters (held until DelayedLoadFinished) m_ScriptFile = scriptFile; pSimulation2 = pSimulation2_; pSimContext = pSimulation2 ? &pSimulation2->GetSimContext() : NULL; @@ -207,8 +203,6 @@ // load map settings script (must be done after reading map) RegMemFun(this, &CMapReader::LoadMapSettings, L"CMapReader::LoadMapSettings", 5); - - RegMemFun(this, &CMapReader::DelayLoadFinished, L"CMapReader::DelayLoadFinished", 5); } // UnpackMap: unpack the given data from the raw data stream into local variables @@ -1213,14 +1207,6 @@ return ret; } -int CMapReader::DelayLoadFinished() -{ - // we were dynamically allocated by CWorld::Initialize - delete this; - - return 0; -} - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// Index: source/ps/Game.cpp =================================================================== --- source/ps/Game.cpp +++ source/ps/Game.cpp @@ -293,6 +293,8 @@ JSContext* cx = m_Simulation2->GetScriptInterface().GetContext(); JSAutoRequest rq(cx); + m_World->DeleteMapReader(); + // Call the script function InitGame only for new games, not saved games if (!m_IsSavedGame) { Index: source/ps/World.h =================================================================== --- source/ps/World.h +++ source/ps/World.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -39,6 +39,7 @@ class CTerritoryManager; class CTerrain; class CStrW; +class CMapReader; /** * CWorld is a general data class containing whatever is needed to accurately represent the world. @@ -66,6 +67,8 @@ **/ CTerritoryManager *m_TerritoryManager; + CMapReader* m_MapReader; + public: CWorld(CGame *pGame); ~CWorld(); @@ -81,6 +84,11 @@ void RegisterInitRMS(const CStrW& scriptFile, JSRuntime* rt, JS::HandleValue settings, int playerID); /** + * Explicitly delete m_MapReader after the map has finished loading. + **/ + void DeleteMapReader(); + + /** * Get the pointer to the terrain object. * * @return CTerrain * the value of m_Terrain. Index: source/ps/World.cpp =================================================================== --- source/ps/World.cpp +++ source/ps/World.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -58,7 +58,8 @@ m_pGame(pGame), m_Terrain(new CTerrain()), m_UnitManager(new CUnitManager()), - m_TerritoryManager(NULL) + m_TerritoryManager(NULL), + m_MapReader(new CMapReader) { } @@ -71,13 +72,11 @@ if (mapFile.length()) { VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp"); - CMapReader* reader = 0; try { - reader = new CMapReader; CTriggerManager* pTriggerManager = NULL; - reader->LoadMap(mapfilename, rt, settings, m_Terrain, + m_MapReader->LoadMap(mapfilename, rt, settings, m_Terrain, CRenderer::IsInitialised() ? g_Renderer.GetWaterManager() : NULL, CRenderer::IsInitialised() ? g_Renderer.GetSkyManager() : NULL, &g_LightEnv, m_pGame->GetView(), @@ -88,7 +87,7 @@ } catch (PSERROR_File& err) { - delete reader; + SAFE_DELETE(m_MapReader); LOGERROR("Failed to load map %s: %s", mapfilename.string8(), err.what()); throw PSERROR_Game_World_MapLoadFailed("Failed to load map.\nCheck application log for details."); } @@ -98,11 +97,8 @@ void CWorld::RegisterInitRMS(const CStrW& scriptFile, JSRuntime* rt, JS::HandleValue settings, int playerID) { // If scriptFile is empty, a blank map will be generated using settings (no RMS run) - CMapReader* reader = 0; - - reader = new CMapReader; CTriggerManager* pTriggerManager = NULL; - reader->LoadRandomMap(scriptFile, rt, settings, m_Terrain, + m_MapReader->LoadRandomMap(scriptFile, rt, settings, m_Terrain, CRenderer::IsInitialised() ? g_Renderer.GetWaterManager() : NULL, CRenderer::IsInitialised() ? g_Renderer.GetSkyManager() : NULL, &g_LightEnv, m_pGame->GetView(), @@ -112,6 +108,10 @@ // registers for delay loading } +void CWorld::DeleteMapReader() +{ + SAFE_DELETE(m_MapReader); +} /** * Destructor. @@ -121,4 +121,5 @@ { delete m_Terrain; delete m_UnitManager; + delete m_MapReader; } Index: source/simulation2/Simulation2.cpp =================================================================== --- source/simulation2/Simulation2.cpp +++ source/simulation2/Simulation2.cpp @@ -410,7 +410,7 @@ // Load the map into the secondary simulation LDR_BeginRegistering(); - CMapReader* mapReader = new CMapReader; // automatically deletes itself + std::unique_ptr mapReader(new CMapReader); std::string mapType; scriptInterface.GetProperty(m_InitAttributes, "mapType", mapType); Index: source/simulation2/components/tests/test_Pathfinder.h =================================================================== --- source/simulation2/components/tests/test_Pathfinder.h +++ source/simulation2/components/tests/test_Pathfinder.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2016 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -72,7 +72,7 @@ sim2.LoadDefaultScripts(); sim2.ResetState(); - CMapReader* mapReader = new CMapReader(); // it'll call "delete this" itself + std::unique_ptr mapReader(new CMapReader()); LDR_BeginRegistering(); mapReader->LoadMap(L"maps/skirmishes/Median Oasis (2).pmp", @@ -183,7 +183,7 @@ sim2.LoadDefaultScripts(); sim2.ResetState(); - CMapReader* mapReader = new CMapReader(); // it'll call "delete this" itself + std::unique_ptr mapReader(new CMapReader()); LDR_BeginRegistering(); mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp", @@ -238,7 +238,7 @@ sim2.LoadDefaultScripts(); sim2.ResetState(); - CMapReader* mapReader = new CMapReader(); // it'll call "delete this" itself + std::unique_ptr mapReader(new CMapReader()); LDR_BeginRegistering(); mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp", Index: source/simulation2/tests/test_Serializer.h =================================================================== --- source/simulation2/tests/test_Serializer.h +++ source/simulation2/tests/test_Serializer.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2016 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -839,7 +839,7 @@ sim2.LoadDefaultScripts(); sim2.ResetState(); - CMapReader* mapReader = new CMapReader(); // it'll call "delete this" itself + std::unique_ptr mapReader(new CMapReader()); LDR_BeginRegistering(); mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp",