xLua
xLua copied to clipboard
Xlua中Dispose()释放报错委托回调问题
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