xlua重启虚拟机处理

起因

xlua重启虚拟机时,需要调用Env.Dispose(); 

但会抛出异常"try to dispose a LuaEnv with C# callback!"

if (!translator.AllDelegateBridgeReleased())
{
    throw new InvalidOperationException("try to dispose a LuaEnv with C# callback!");
}

这是因为C#端还持有lua的方法没有被释放,所以想要重启虚拟机必须要先把所有的C#委托清空

还需要注意 

  1. 在同一个方法中释放了delegate的引用,但又立即调用了LuaEnv.Dispose().
  2. 在Lua端调用了Env.Dispose();

 

解决方法

UI部分的事件监听是大头,比如

self._btnMapShift.onClick:AddListener(CT.MainCityCtrl.OnBtnMapShift)
我们可以使用正则表式式 把这种监听替换成统一的方法里处理,然后在方法,把所有的监听都记录下来,需要重启时统一释放

 

统一处理UI的方法代码

function UIAddListenerIndirect(uiEnlement,eventType,func)
    if uiEnlement[eventType] then
        local callInfo = {
            uiEnlement = uiEnlement,
            eventType = eventType,
            callBackFunc = func
        }
        uiEnlement[eventType]:AddListener(func)
        table.insert(D.UIListenerDelegate,callInfo)
    end
end

function UIRemoveListenerIndirect(uiEnlement,eventType,func)
    if uiEnlement[eventType] then
        uiEnlement[eventType]:RemoveListener(func)
    end
end

function UIRemoveAllListener()
    for k,v in pairs(D.UIListenerDelegate) do
        local callInfo = v
        --callInfo.uiDelegate:RemoveListener(callInfo.callBackFunc)
        if callInfo.uiEnlement[callInfo.eventType] then
            callInfo.uiEnlement[callInfo.eventType]:RemoveAllListeners()
            callInfo.uiEnlement[callInfo.eventType]:Invoke("",0)
        end
    end
    D.UIListenerDelegate = {}
end

此外还有两个方法查看C#端没被清空的委托 便于我们单独处理

1 xlua提供了一个lua方法(该方法位于XLua/Resources/xlua/util.lua.txt中): print_func_ref_by_csharp()

function print_func_ref_by_csharp()
    local registry = debug.getregistry()
    for k, v in pairs(registry) do
        if type(k) == 'number' and type(v) == 'function' and registry[v] == k then
            local info = debug.getinfo(v)
            print(string.format('%s:%d', info.short_src, info.linedefined))
        end
    end
end

2 修改ObjectTranslator.cs 在注入委托时 记录Lua栈信息,在AllDelegateBridgeReleased时打印出来

        Dictionary<int, WeakReference> delegate_bridges = new Dictionary<int, WeakReference>();

#if UNITY_EDITOR
     
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值