diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index daf93ce8b5..01e20a8f7f 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -159,6 +159,11 @@ namespace BizHawk.Client.EmuHawk EventsLibrary.CurrentThread = script; _currThread = script; var execResult = script.Resume(0); + + _lua.RunScheduledDisposes(); + //not sure how this is going to work out, so do this too + script.RunScheduledDisposes(); + _currThread = null; var result = new ResumeResult(); if (execResult == 0) diff --git a/LuaInterface/LuaInterface/Lua.cs b/LuaInterface/LuaInterface/Lua.cs index 152778b97d..0a15fd59f0 100644 --- a/LuaInterface/LuaInterface/Lua.cs +++ b/LuaInterface/LuaInterface/Lua.cs @@ -959,6 +959,26 @@ namespace LuaInterface #endregion + List scheduledDisposes = new List(); + + internal void ScheduleDispose(int reference) + { + //TODO - theres a race condition here, see comment elsewhere + lock (scheduledDisposes) + scheduledDisposes.Add(reference); + } + + public void RunScheduledDisposes() + { + //TODO - theres a race condition here, in case GC happens during this method + lock (scheduledDisposes) + { + foreach (var item in scheduledDisposes) + dispose(item); + scheduledDisposes.Clear(); + } + } + internal void dispose(int reference) { if (luaState != IntPtr.Zero) //Fix submitted by Qingrui Li diff --git a/LuaInterface/LuaInterface/LuaBase.cs b/LuaInterface/LuaInterface/LuaBase.cs index 10ae05347e..dca7041420 100644 --- a/LuaInterface/LuaInterface/LuaBase.cs +++ b/LuaInterface/LuaInterface/LuaBase.cs @@ -16,7 +16,7 @@ namespace LuaInterface ~LuaBase() { //Dispose(false); - Dispose(true); //zero 28-feb-2014 - fix memory leak? + Dispose(true); //zero 28-feb-2015 - fix memory leak? } public void Dispose() @@ -31,8 +31,8 @@ namespace LuaInterface { if (disposeManagedResources) { - if (_Reference != 0) - _Interpreter.dispose(_Reference); + if (_Reference != 0 && _Interpreter != null) + _Interpreter.ScheduleDispose(_Reference); } _Interpreter = null; _Disposed = true; diff --git a/References/LuaInterface.dll b/References/LuaInterface.dll index 6065155529..46bdb94093 100644 Binary files a/References/LuaInterface.dll and b/References/LuaInterface.dll differ