• <bdo id='U08aB'></bdo><ul id='U08aB'></ul>
    <legend id='U08aB'><style id='U08aB'><dir id='U08aB'><q id='U08aB'></q></dir></style></legend>
  1. <tfoot id='U08aB'></tfoot>

    <small id='U08aB'></small><noframes id='U08aB'>

  2. <i id='U08aB'><tr id='U08aB'><dt id='U08aB'><q id='U08aB'><span id='U08aB'><b id='U08aB'><form id='U08aB'><ins id='U08aB'></ins><ul id='U08aB'></ul><sub id='U08aB'></sub></form><legend id='U08aB'></legend><bdo id='U08aB'><pre id='U08aB'><center id='U08aB'></center></pre></bdo></b><th id='U08aB'></th></span></q></dt></tr></i><div id='U08aB'><tfoot id='U08aB'></tfoot><dl id='U08aB'><fieldset id='U08aB'></fieldset></dl></div>

    1. 在 luabind::object 中存储带有父级的 lua 类

      时间:2023-09-26

          <tfoot id='Gcbm6'></tfoot>

          <small id='Gcbm6'></small><noframes id='Gcbm6'>

          <i id='Gcbm6'><tr id='Gcbm6'><dt id='Gcbm6'><q id='Gcbm6'><span id='Gcbm6'><b id='Gcbm6'><form id='Gcbm6'><ins id='Gcbm6'></ins><ul id='Gcbm6'></ul><sub id='Gcbm6'></sub></form><legend id='Gcbm6'></legend><bdo id='Gcbm6'><pre id='Gcbm6'><center id='Gcbm6'></center></pre></bdo></b><th id='Gcbm6'></th></span></q></dt></tr></i><div id='Gcbm6'><tfoot id='Gcbm6'></tfoot><dl id='Gcbm6'><fieldset id='Gcbm6'></fieldset></dl></div>
            <tbody id='Gcbm6'></tbody>

            <legend id='Gcbm6'><style id='Gcbm6'><dir id='Gcbm6'><q id='Gcbm6'></q></dir></style></legend>
            • <bdo id='Gcbm6'></bdo><ul id='Gcbm6'></ul>
              • 本文介绍了在 luabind::object 中存储带有父级的 lua 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                使用C++lua 5.1luabind 0.7-0.81

                尝试创建一个带有父类的 lua 类并将其存储在 luabind::object 中.

                Lua

                class 'TestClassParent'函数 TestClassParent:__init()打印('父初始化
                ')结尾函数 TestClassParent:__finalize()打印('父完成
                ')结尾类TestClass"(TestClassParent)函数测试类:__init()打印('初始化
                ')TestClassParent.__init(self)结尾函数 TestClass:__finalize()打印('完成
                ')结尾

                C++

                <代码>{luabind::object obj = luabind::call_function(lua_state, "TestClass");}printf("GC前
                ");lua_gc(lua, LUA_GCCOLLECT, 0);printf("GC后
                ");

                输出:
                初始化
                父初始化
                GC前
                GC后

                结果: obj 被销毁后,'TestClass' 实例在垃圾回收周期后仍然活着(__finalize 方法未被调用,内存未被释放).它仅在程序退出时销毁.
                Moresome 如果我在没有父级的情况下使用类,则会正确收集垃圾.

                如果我尝试使用采用策略(获取创建对象的所有权)

                luabind::object obj = luabind::call_function(lua_state, "TestClass")[luabind::adopt(luabind::result)];

                我明白了:

                • luabind 0.7 中 - 与不采用策略的结果相同
                • luabind 0.81 中 - 崩溃并显示消息您正在尝试使用未注册类型"

                我怎样才能正确在 C++ 中创建一个 lua 对象取得它的所有权?

                解决方案

                这是 0.8.1 中的已知错误;对最后构造对象的引用保留在超级"函数 upvalue 中.它已在 0.9-rc1 中修复:

                http://github.com/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3" rel="noreferrer">http://github.com/luabind/luabind/commit/2c99f0475c283c27fc9fd275243c77fd27524ac70/p>

                Using C++, lua 5.1, luabind 0.7-0.81

                Trying to create a lua class with parent and store it in a luabind::object.

                Lua

                class 'TestClassParent'
                function TestClassParent:__init()
                    print('parent init
                ')
                end
                function TestClassParent:__finalize()
                    print('parent finalize
                ')
                end
                
                class 'TestClass' (TestClassParent)
                function TestClass:__init()
                    print('init
                ')
                    TestClassParent.__init(self)
                end
                function TestClass:__finalize()
                    print('finalize
                ')
                end
                

                C++

                {
                    luabind::object obj = luabind::call_function<luabind::object>(lua_state, "TestClass");
                }
                printf("before GC
                ");
                lua_gc(lua, LUA_GCCOLLECT, 0);
                printf("after GC
                ");
                

                Output:
                init
                parent init
                before GC
                after GC

                Result: After obj is destroyed, 'TestClass' instance is still alive after garbage collection cycle (__finalize method is not called and memory is not freed). It's destroying only on program exit.
                Moresome if I use class without parent, garbage is collected correctly.

                If I try to use adopt policy (to take ownership of created object)

                luabind::object obj = luabind::call_function<luabind::object>(lua_state, "TestClass")[luabind::adopt(luabind::result)];
                

                I get:

                • in luabind 0.7 - same result as without adopt policy
                • in luabind 0.81 - crash with message "you are trying to use an unregistrerd type"

                How can I correctly create a lua object in C++ and take it's ownership?

                解决方案

                This is a known bug in 0.8.1; a reference to the last constructed object is left in the "super" function upvalue. It has been fixed in 0.9-rc1:

                http://github.com/luabind/luabind/commit/2c99f0475afea7c282c2e432499fd22aa17744e3

                这篇关于在 luabind::object 中存储带有父级的 lua 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:通过 Swig 在 C++ 和 Lua 之间传递变量 下一篇:Luabridge 绑定重载运算符

                相关文章

                <small id='Nq2wO'></small><noframes id='Nq2wO'>

                  <bdo id='Nq2wO'></bdo><ul id='Nq2wO'></ul>

                1. <legend id='Nq2wO'><style id='Nq2wO'><dir id='Nq2wO'><q id='Nq2wO'></q></dir></style></legend>

                  1. <i id='Nq2wO'><tr id='Nq2wO'><dt id='Nq2wO'><q id='Nq2wO'><span id='Nq2wO'><b id='Nq2wO'><form id='Nq2wO'><ins id='Nq2wO'></ins><ul id='Nq2wO'></ul><sub id='Nq2wO'></sub></form><legend id='Nq2wO'></legend><bdo id='Nq2wO'><pre id='Nq2wO'><center id='Nq2wO'></center></pre></bdo></b><th id='Nq2wO'></th></span></q></dt></tr></i><div id='Nq2wO'><tfoot id='Nq2wO'></tfoot><dl id='Nq2wO'><fieldset id='Nq2wO'></fieldset></dl></div>
                    <tfoot id='Nq2wO'></tfoot>