File tree Expand file tree Collapse file tree 3 files changed +21
-2
lines changed Expand file tree Collapse file tree 3 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -92,14 +92,24 @@ void Lua::Shutdown()
9292
9393void 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
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ GarrysMod::Lua::ILuaInterface* g_Lua;
1616IVEngineServer* engine;
1717CGlobalEntityList* Util::entitylist = NULL ;
1818CUserMessages* Util::pUserMessages;
19+ std::unordered_set<LuaUserData*> g_pLuaUserData;
1920
2021std::unordered_set<int > Util::g_pReference;
2122ConVar Util::holylib_debug_mainutil (" holylib_debug_mainutil" , " 1" );
Original file line number Diff line number Diff 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;
223225struct 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)
You can’t perform that action at this time.
0 commit comments