neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
466 stars 76 forks source link

No proper dispose of delegates at new environment creation. #184

Closed Falkitop closed 5 months ago

Falkitop commented 5 months ago

NeoLua Version: v1.3.14 Example to reproduce:

local Timer = clr.System.Timers.Timer
local MyTimer = Timer()

MyTimer.Enabled = true
MyTimer.AutoReset = true
MyTimer.Interval = 10
MyTimer.Elapsed:add(function(sender, args) : void
    print("a")
end)
Console.Clear();
LuaGlobal env = LuaState.CreateEnvironment();
LuaResult res = env.DoChunk(scriptpath);

Changing the MyTimer.Enabled = false still executes the Elapsed function. The script engine is caching the calls and requires, but is it wanted behavior to also cache delegates?

            foreach(var ls in LuaStates)
            {
                ls.Dispose();
                ls.Clear();
            }
            try
            {
                using (Lua LuaState = new Lua())
                {
                    Console.Clear();
                    LuaGlobal env = LuaState.CreateEnvironment();
                    LuaResult res = env.DoChunk(scriptpath);
                    LuaStates.Add(LuaState);
                }

            }
            catch (LuaException ex)
            {
                Console.WriteLine(ex.ToString(), 10);
            }

Even after proper dispose, the delegate is still persistent.