Skip to content

Commit e3680b7

Browse files
committed
Fix memory leak in CScriptEngine.
1 parent 16987f7 commit e3680b7

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

src/xrScriptEngine/script_engine.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,14 @@ bool RunJITCommand(lua_State* ls, const char* command)
9898

9999
const char* const CScriptEngine::GlobalNamespace = SCRIPT_GLOBAL_NAMESPACE;
100100
Lock CScriptEngine::stateMapLock;
101-
xr_unordered_map<lua_State*, CScriptEngine*>* CScriptEngine::stateMap = nullptr;
101+
xr_unordered_map<lua_State*, CScriptEngine*> CScriptEngine::stateMap;
102102

103103
string4096 CScriptEngine::g_ca_stdout;
104104

105105
void CScriptEngine::reinit()
106106
{
107107
stateMapLock.Enter();
108-
if (!stateMap)
109-
{
110-
stateMap = new xr_unordered_map<lua_State*, CScriptEngine*>();
111-
stateMap->reserve(32); // 32 lua states should be enough
112-
}
108+
stateMap.reserve(32); // 32 lua states should be enough
113109
stateMapLock.Leave();
114110
if (m_virtual_machine)
115111
{
@@ -1192,12 +1188,9 @@ CScriptEngine* CScriptEngine::GetInstance(lua_State* state)
11921188
{
11931189
CScriptEngine* instance = nullptr;
11941190
stateMapLock.Enter();
1195-
if (stateMap)
1196-
{
1197-
auto it = stateMap->find(state);
1198-
if (it != stateMap->end())
1199-
instance = it->second;
1200-
}
1191+
auto it = stateMap.find(state);
1192+
if (it != stateMap.end())
1193+
instance = it->second;
12011194
stateMapLock.Leave();
12021195
return instance;
12031196
}
@@ -1206,14 +1199,11 @@ bool CScriptEngine::RegisterState(lua_State* state, CScriptEngine* scriptEngine)
12061199
{
12071200
bool result = false;
12081201
stateMapLock.Enter();
1209-
if (stateMap)
1202+
auto it = stateMap.find(state);
1203+
if (it == stateMap.end())
12101204
{
1211-
auto it = stateMap->find(state);
1212-
if (it == stateMap->end())
1213-
{
1214-
stateMap->insert({state, scriptEngine});
1215-
result = true;
1216-
}
1205+
stateMap.insert({state, scriptEngine});
1206+
result = true;
12171207
}
12181208
stateMapLock.Leave();
12191209
return result;
@@ -1225,14 +1215,11 @@ bool CScriptEngine::UnregisterState(lua_State* state)
12251215
return true;
12261216
bool result = false;
12271217
stateMapLock.Enter();
1228-
if (stateMap)
1218+
auto it = stateMap.find(state);
1219+
if (it != stateMap.end())
12291220
{
1230-
auto it = stateMap->find(state);
1231-
if (it != stateMap->end())
1232-
{
1233-
stateMap->erase(it);
1234-
result = true;
1235-
}
1221+
stateMap.erase(it);
1222+
result = true;
12361223
}
12371224
stateMapLock.Leave();
12381225
return result;

src/xrScriptEngine/script_engine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class XRSCRIPTENGINE_API CScriptEngine
7575

7676
private:
7777
static Lock stateMapLock;
78-
static xr_unordered_map<lua_State*, CScriptEngine*>* stateMap;
78+
static xr_unordered_map<lua_State*, CScriptEngine*> stateMap;
7979
lua_State* m_virtual_machine;
8080
CScriptThread* m_current_thread;
8181
bool m_reload_modules;

0 commit comments

Comments
 (0)