Skip to content

Commit acb3ccb

Browse files
committed
util: try to check for leaked userdata?!?
related to #27
1 parent e1c5849 commit acb3ccb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

source/lua.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,24 @@ void Lua::Shutdown()
9292

9393
void Lua::FinalShutdown()
9494
{
95+
// g_Lua is bad at this point / the lua_State is already gone so we CAN'T allow any calls
9596
g_Lua = NULL;
96-
97+
98+
for (auto& ref : g_pLuaUserData)
99+
{
100+
if (Util::holylib_debug_mainutil.GetBool())
101+
Msg("holylib: Discarding of old userdata %p\n", ref);
102+
103+
// if g_Lua was not NULL it would attempt to free the references.
104+
delete ref;
105+
}
106+
g_pLuaUserData.clear();
107+
97108
for (auto& ref : Util::g_pReference)
98109
{
99110
if (Util::holylib_debug_mainutil.GetBool())
100111
Msg("holylib: Discarding of old reference %i\n", ref);
101112
}
102-
103113
Util::g_pReference.clear();
104114
}
105115

source/util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ GarrysMod::Lua::ILuaInterface* g_Lua;
1616
IVEngineServer* engine;
1717
CGlobalEntityList* Util::entitylist = NULL;
1818
CUserMessages* Util::pUserMessages;
19+
std::unordered_set<LuaUserData*> g_pLuaUserData;
1920

2021
std::unordered_set<int> Util::g_pReference;
2122
ConVar Util::holylib_debug_mainutil("holylib_debug_mainutil", "1");

source/util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,22 @@ namespace Util
220220
ToDo: Implement a proper class like gmod has with CLuaCLass/CLuaLibrary & use thoes instead for everything.
221221
*/
222222

223+
struct LuaUserData;
224+
extern std::unordered_set<LuaUserData*> g_pLuaUserData;
223225
struct LuaUserData {
226+
LuaUserData() {
227+
g_pLuaUserData.insert(this);
228+
}
229+
224230
~LuaUserData() {
225231
if (!ThreadInMainThread())
226232
{
227233
Warning("holylib: Tried to delete usetdata from another thread!\n");
228234
return;
229235
}
230236

237+
g_pLuaUserData.erase(this);
238+
231239
if (iReference != -1)
232240
{
233241
if (g_Lua)

0 commit comments

Comments
 (0)