找回密码
 立即注册
查看: 308|回复: 0

unity-tolua之释放引用实测

[复制链接]
发表于 2022-4-13 15:24 | 显示全部楼层 |阅读模式

title: unity-tolua之释放引用实测
categories: Unity3d
tags: [unity, tolua]
date: 2021-12-11 17:08:58
comments: false
mathjax: true
toc: true


unity-tolua之释放引用实测, 主要是验证是否能够解开 lua 与 csharp 之间的引用, 进而被对应 gc 回收.

前篇

    unity-tolua之Dispose释放引用
目的是测试 csharp 中的 lua 相关导出变量在不 Dispose 的情况下是否会解引用, 进而被 gc.

相关测试代码


  • csharp
    1. publicclassCDog:MonoBehaviour{publicLuaFunction luaCellCreateFn;voidOnDestroy(){if(luaCellCreateFn !=null){
    2.             luaCellCreateFn.Dispose();// 主要是测试 Dispose
    3.             luaCellCreateFn =null;}}}
    复制代码
  • lua
    1. -- 类定义local CGm =class()
    2. CGm.__name ="CGm"function CGm.Init(self)endreturn CGm
    3. -- lua 对象被回收会调用这个函数, obj 是实例对象, 这个代码就不贴那么细了function_luaObjGc(obj)local type_id = obj.__name
    4.         if type_id thengLog("--- _luaObjGc, name: {0}", type_id)endend--- 测试逻辑function_test()local testIns =require("gm"):New()-- 创建 CGm 实例local dog = GameMgr.Ins:AddComponent("CDog")
    5.     dog.luaCellCreateFn =function()-- 绑定 lua 方法到 csharp 中
    6.         testIns:Init()-- 引用住 testInsendend
    复制代码

确定实例对象及方法都是引用住的

    执行 _test 方法, 会往 go 上挂一个 CDog 实例组件, 同时 绑定了 luaCellCreateFn 方法, luaCellCreateFn 方法右引用住了 testIns 实例
    触发 lua gc 几次


    没有发现 testIns 实例被回收的日志, 这是正确的, 因为被引用住了

不 Dispose LuaFunction

    注释掉 csharp 中 CDog 的 OnDestroy 方法中的 luaCellCreateFn.Dispose(); 代码
    在 lua 中销毁 dog 实例
    1. GameObject.Destroy(dog)
    复制代码
    触发 lua gc 几次 (不要太多次, 不要触发到 csharp 的 gc)


    发现了其他 lua 实例被回收的日志, 但没有发现 testIns 实例被回收的日志
    触发 csharp gc 几次 再触发 lua gc 几次


    发现 testIns 实例被回收的日志
    原因是因为 csharp 中 CDog 实例被 gc 后, 才会调用到 tolua 框架的 析构函数 进行解引用, 解引用完后绑定到 luaCellCreateFn 的 lua 方法才被释放, 然后进一步回收 testIns 实例

主动 Dispose LuaFunction

    打开 csharp 中 CDog 的 OnDestroy 方法中的 luaCellCreateFn.Dispose(); 代码
    在 lua 中销毁 dog 实例
    1. GameObject.Destroy(dog)
    复制代码
    触发 lua gc 几次


    发现 testIns 实例被回收的日志, 而且还在其他 lua 实例被回收之前
    原因是因为 CDog 的 OnDestroy 方法中的 luaCellCreateFn.Dispose(); 代码, 主动解开了 lua 中绑定的 luaCellCreateFn 方法引用, 然 lua gc 时就可以立马释放 luaCellCreateFn 方法 及 testIns 实例

结论

在 csharp 中不主动 Dispose lua 方法/table 都是没问题的, 只是被回收的时间会比较长
如果主动 Dispose, 就能及时回收 lua 实例.
by the way, 养成良好习惯, 在不使用时都主动置空 (lua 置为 nil, csharp 置为 null)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-7 15:28 , Processed in 0.136930 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表