Skip to content

Commit eec4a57

Browse files
committed
Improve resource management in CAI_Space
Raw pointers are changed to unique_ptr's, init() method becomes private, instance of the class becomes static, prohibited copy construction and assignment.
1 parent 378e69f commit eec4a57

File tree

9 files changed

+70
-73
lines changed

9 files changed

+70
-73
lines changed

src/utils/xrSE_Factory/ai_space.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111
#include "xrScriptEngine/script_engine.hpp"
1212
#include "xrServerEntities/object_factory.h"
1313

14-
CAI_Space* g_ai_space = nullptr;
14+
static CAI_Space g_ai_space;
1515

16-
CAI_Space::CAI_Space() { m_script_engine = nullptr; }
16+
CAI_Space& CAI_Space::GetInstance()
17+
{
18+
auto& instance = g_ai_space;
19+
if (!instance.m_inited)
20+
{
21+
instance.init();
22+
}
23+
return instance;
24+
}
1725

1826
void CAI_Space::RegisterScriptClasses()
1927
{
@@ -49,13 +57,16 @@ void CAI_Space::RegisterScriptClasses()
4957

5058
void CAI_Space::init()
5159
{
52-
VERIFY(!m_script_engine);
60+
R_ASSERT(!m_inited);
61+
5362
VERIFY(!GEnv.ScriptEngine);
54-
GEnv.ScriptEngine = m_script_engine = new CScriptEngine(true);
63+
GEnv.ScriptEngine = new CScriptEngine(true);
5564
XRay::ScriptExporter::Reset(); // mark all nodes as undone
56-
m_script_engine->init(XRay::ScriptExporter::Export, true);
65+
GEnv.ScriptEngine->init(XRay::ScriptExporter::Export, true);
5766
RegisterScriptClasses();
5867
object_factory().register_script();
68+
69+
m_inited = true;
5970
}
6071

61-
CAI_Space::~CAI_Space() { xr_delete(m_script_engine); }
72+
CAI_Space::~CAI_Space() { xr_delete(GEnv.ScriptEngine); }

src/utils/xrSE_Factory/ai_space.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ class CScriptEngine;
1313
class CAI_Space
1414
{
1515
private:
16-
CScriptEngine* m_script_engine;
16+
bool m_inited = false;
1717

18+
void init();
1819
void RegisterScriptClasses();
1920

2021
public:
21-
CAI_Space();
22+
CAI_Space() = default;
23+
CAI_Space(const CAI_Space&) = delete;
24+
CAI_Space& operator=(const CAI_Space&) = delete;
2225
virtual ~CAI_Space();
23-
void init();
26+
static CAI_Space& GetInstance();
27+
2428
IC CScriptEngine& script_engine() const;
2529
};
2630

27-
extern CAI_Space* g_ai_space;
28-
2931
IC CAI_Space& ai();
3032

3133
#include "ai_space_inline.h"

src/utils/xrSE_Factory/ai_space_inline.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@
1010

1111
IC CScriptEngine& CAI_Space::script_engine() const
1212
{
13-
VERIFY(m_script_engine);
14-
return *m_script_engine;
13+
VERIFY(GEnv.ScriptEngine);
14+
return *GEnv.ScriptEngine;
1515
}
1616

17-
IC CAI_Space& ai()
18-
{
19-
if (!g_ai_space)
20-
{
21-
g_ai_space = new CAI_Space();
22-
g_ai_space->init();
23-
}
24-
return *g_ai_space;
25-
}
17+
IC CAI_Space& ai() { return CAI_Space::GetInstance(); }

src/utils/xrSE_Factory/xrSE_Factory.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ BOOL APIENTRY DllMain(HANDLE module_handle, DWORD call_reason, LPVOID reserved)
6060
auto s = (CInifile**)&pSettings;
6161
xr_delete(*s);
6262
xr_delete(g_property_list_helper);
63-
xr_delete(g_ai_space);
6463
xr_delete(g_object_factory);
6564
if (prop_helper_module)
6665
prop_helper_module = nullptr;

src/xrGame/Level.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ CLevel::CLevel()
110110
Msg("%s", Core.Params);
111111
}
112112

113-
extern CAI_Space* g_ai_space;
114-
115113
CLevel::~CLevel()
116114
{
117115
xr_delete(g_player_hud);

src/xrGame/ai_space.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,42 @@
2222
#include "moving_objects.h"
2323
#include "doors_manager.h"
2424

25-
CAI_Space* g_ai_space = 0;
25+
static CAI_Space g_ai_space;
2626

27-
CAI_Space::CAI_Space()
27+
CAI_Space& CAI_Space::GetInstance()
2828
{
29-
m_ef_storage = 0;
30-
m_cover_manager = 0;
31-
m_alife_simulator = 0;
32-
m_moving_objects = 0;
33-
m_doors_manager = 0;
29+
auto& instance = g_ai_space;
30+
if (!instance.m_inited)
31+
{
32+
instance.init();
33+
}
34+
return instance;
3435
}
3536

3637
void CAI_Space::init()
3738
{
38-
if (GEnv.isDedicatedServer)
39-
return;
40-
AISpaceBase::Initialize();
41-
VERIFY(!m_ef_storage);
42-
m_ef_storage = new CEF_Storage();
43-
44-
VERIFY(!m_cover_manager);
45-
m_cover_manager = new CCoverManager();
46-
47-
VERIFY(!m_moving_objects);
48-
m_moving_objects = new ::moving_objects();
49-
VERIFY(!GEnv.ScriptEngine);
50-
GEnv.ScriptEngine = new CScriptEngine();
51-
SetupScriptEngine();
39+
R_ASSERT(!m_inited);
40+
41+
if (!GEnv.isDedicatedServer)
42+
{
43+
AISpaceBase::Initialize();
44+
45+
m_ef_storage = std::make_unique<CEF_Storage>();
46+
m_cover_manager = std::make_unique<CCoverManager>();
47+
m_moving_objects = std::make_unique<::moving_objects>();
48+
49+
VERIFY(!GEnv.ScriptEngine);
50+
GEnv.ScriptEngine = new CScriptEngine();
51+
SetupScriptEngine();
52+
}
53+
54+
m_inited = true;
5255
}
5356

5457
CAI_Space::~CAI_Space()
5558
{
5659
unload();
5760
xr_delete(GEnv.ScriptEngine); // XXX: wrapped into try..catch(...) in vanilla source
58-
xr_delete(m_doors_manager);
59-
xr_delete(m_moving_objects);
60-
xr_delete(m_cover_manager);
61-
xr_delete(m_ef_storage);
6261
}
6362

6463
void CAI_Space::RegisterScriptClasses()
@@ -147,8 +146,7 @@ void CAI_Space::load(LPCSTR level_name)
147146
m_cover_manager->compute_static_cover();
148147
m_moving_objects->on_level_load();
149148

150-
VERIFY(!m_doors_manager);
151-
m_doors_manager = new ::doors::manager(level_graph().header().box());
149+
m_doors_manager.reset(new ::doors::manager(level_graph().header().box()));
152150

153151
#ifdef DEBUG
154152
Msg("* Loading ai space is successfully completed (%.3fs, %7.3f Mb)", timer.GetElapsed_sec(),
@@ -161,7 +159,7 @@ void CAI_Space::unload(bool reload)
161159
if (GEnv.isDedicatedServer)
162160
return;
163161
GEnv.ScriptEngine->unload();
164-
xr_delete(m_doors_manager);
162+
m_doors_manager.reset(nullptr);
165163
AISpaceBase::Unload(reload);
166164
}
167165

src/xrGame/ai_space.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "xrAICore/AISpaceBase.hpp"
1212

13+
#include <memory>
14+
1315
class CGameGraph;
1416
class CGameLevelCrossTable;
1517
class CLevelGraph;
@@ -36,23 +38,30 @@ class CAI_Space : public AISpaceBase
3638
friend class CLevel;
3739

3840
private:
39-
CEF_Storage* m_ef_storage;
40-
CALifeSimulator* m_alife_simulator;
41-
CCoverManager* m_cover_manager;
42-
moving_objects* m_moving_objects;
43-
doors::manager* m_doors_manager;
41+
bool m_inited = false;
42+
43+
std::unique_ptr<CEF_Storage> m_ef_storage;
44+
std::unique_ptr<CCoverManager> m_cover_manager;
45+
std::unique_ptr<moving_objects> m_moving_objects;
46+
std::unique_ptr<doors::manager> m_doors_manager;
47+
48+
CALifeSimulator* m_alife_simulator = nullptr;
4449

4550
private:
51+
void init();
4652
void load(LPCSTR level_name);
4753
void unload(bool reload = false);
4854
void set_alife(CALifeSimulator* alife_simulator);
4955
void LoadCommonScripts();
5056
void RegisterScriptClasses();
5157

5258
public:
53-
CAI_Space();
59+
CAI_Space() = default;
60+
CAI_Space(const CAI_Space&) = delete;
61+
CAI_Space& operator=(const CAI_Space&) = delete;
5462
virtual ~CAI_Space();
55-
void init();
63+
static CAI_Space& GetInstance();
64+
5665
void SetupScriptEngine();
5766
IC CEF_Storage& ef_storage() const;
5867

@@ -65,6 +74,4 @@ class CAI_Space : public AISpaceBase
6574

6675
IC CAI_Space& ai();
6776

68-
extern CAI_Space* g_ai_space;
69-
7077
#include "ai_space_inline.h"

src/xrGame/ai_space_inline.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,4 @@ IC doors::manager& CAI_Space::doors() const
3939
return (*m_doors_manager);
4040
}
4141

42-
IC CAI_Space& ai()
43-
{
44-
if (!g_ai_space)
45-
{
46-
g_ai_space = new CAI_Space();
47-
g_ai_space->init();
48-
}
49-
return (*g_ai_space);
50-
}
42+
IC CAI_Space& ai() { return CAI_Space::GetInstance(); }

src/xrGame/xrgame_dll_detach.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ extern void destroy_lua_wpn_params();
6363
void clean_game_globals()
6464
{
6565
destroy_lua_wpn_params();
66-
// destroy ai space
67-
xr_delete(g_ai_space);
6866
// destroy object factory
6967
xr_delete(g_object_factory);
7068
// destroy monster squad global var

0 commit comments

Comments
 (0)