Tencent / xLua

xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc.
Other
9.32k stars 2.44k forks source link

Xlua中Dispose()释放报错委托回调问题 #1150

Open 27657237 opened 1 month ago

27657237 commented 1 month ago

public delegate void LuaFunction(); [GCOptimize] public struct LuaBootstrap { public LuaFunction Start; public LuaFunction Update; public LuaFunction OnDestroy; }

public class Bootstrap : MonoBehaviour { private LuaBootstrap bootstrap;

void Start()
{
    XluaEnv.Instance.DoString("require('Bootstrap')");
    bootstrap= XluaEnv.Instance.Global.Get<LuaBootstrap>("Bootstrap");
    bootstrap.Start();
}
private void OnDestroy()
{
    bootstrap.OnDestroy();

    bootstrap.Start = null;
    bootstrap.Update = null;
    bootstrap.OnDestroy = null;

    // 最后释放 LuaEnv
    XluaEnv.Instance.Free();
}  

上面是一个简单的生命周期代码 我不知道为什么,我如果在OnDestroy方法执行一次bootstrap.OnDestroy();哪怕后续将他设置为null,在回收还会报错 (上面执行 bootstrap其他委托在设置null还是会报错,Free()方法是单例类里的释放xluaenv方法,无其他逻辑)

报错内容为: InvalidOperationException: try to dispose a LuaEnv with C# callback! XLua.LuaEnv.Dispose (System.Boolean dispose) (at Assets/ThirdParty/XLua/Src/LuaEnv.cs:424) XLua.LuaEnv.Dispose () (at Assets/ThirdParty/XLua/Src/LuaEnv.cs:393) XluaEnv.Free () (at Assets/Script/Tool/XluaEnv.cs:39) Bootstrap.OnDestroy () (at Assets/Script/Bootstrap.cs:40)

Lua源码: Bootstrap={} function Bootstrap.Start() print("Lua:Start") end function Bootstrap.Update() print("Lua:Update") end function Bootstrap.OnDestroy() print("Lua:OnDestroy") end

27657237 commented 1 month ago

请大佬帮我解惑,这个代码是我跟一个很老的教程学习的xlua使用方法,我并不知道这个使用方法目前是否已经过时