Skip to content

Commit 1e36a0e

Browse files
committed
[Repo] Shutdown Lua properly & Fix EntityList possibly causing a crash
1 parent 1c0cbc5 commit 1e36a0e

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

source/lua.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ void Lua::FinalShutdown()
7171
g_Lua = NULL;
7272
}
7373

74+
void Lua::ManualShutdown()
75+
{
76+
if (!g_Lua)
77+
return;
78+
79+
Lua::Shutdown();
80+
Lua::FinalShutdown();
81+
}
82+
7483
static bool bManualShutdown = false;
7584
static Detouring::Hook detour_InitLuaClasses;
7685
static void hook_InitLuaClasses(GarrysMod::Lua::ILuaInterface* LUA) // ToDo: Add a hook to Lua::Startup or whatever it's name was and use that for the ServerInit.

source/lua.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Lua
1616
extern bool PushHook(const char* pName);
1717
extern void AddDetour();
1818
extern void SetManualShutdown();
19+
extern void ManualShutdown();
1920
extern GarrysMod::Lua::ILuaInterface* GetRealm(unsigned char);
2021
extern GarrysMod::Lua::ILuaShared* GetShared();
2122

source/modules/entitylist.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ EntityList::~EntityList()
4040
void EntityList::Clear()
4141
{
4242
pEntities.clear();
43-
for (auto& [_, iReference] : pEntReferences)
44-
g_Lua->ReferenceFree(iReference);
43+
if (g_Lua)
44+
for (auto& [_, iReference] : pEntReferences)
45+
g_Lua->ReferenceFree(iReference);
4546

4647
pEntReferences.clear();
4748
}
4849

4950
void EntityList::CreateReference(CBaseEntity* pEntity)
5051
{
52+
if (!g_Lua)
53+
Error("holylib: missing g_Lua!\n");
54+
5155
Util::Push_Entity(pEntity);
5256
pEntReferences[pEntity] = g_Lua->ReferenceCreate();
5357
}
@@ -58,7 +62,8 @@ void EntityList::FreeEntity(CBaseEntity* pEntity)
5862
if (it != pEntReferences.end())
5963
{
6064
if (IsValidReference(it->second))
61-
g_Lua->ReferenceFree(it->second);
65+
if (g_Lua)
66+
g_Lua->ReferenceFree(it->second);
6267

6368
Vector_RemoveElement(pEntities, pEntity);
6469
pEntReferences.erase(it);

source/plugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void CServerPlugin::Unload(void)
176176
}
177177

178178
HolyLua::Shutdown();
179+
Lua::ManualShutdown(); // Called to make sure that everything is shut down properly in cases of plugin_unload. does nothing if g_Lua is already NULL.
179180
g_pModuleManager.Shutdown();
180181
Util::RemoveDetour();
181182
Detour::Remove(0);

0 commit comments

Comments
 (0)