tolua++中暴露对象给lua时,一定要把析构函数暴露给lua


题目不知道怎么取才好,但是意思很简单:

如果你暴露一个复杂对象给Lua,实现类似于OO编程的话,那么也要把析构函数暴露给Lua.

否则的话,lua gc的时候,回收垃圾对象,没有找到回收函数,就直接free掉了,这在C++中,是相当致命的.

 

tolua++中的tolua_cclass函数,用来注册lua对象

TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col)

 同时会把最后的那个参数col,注册到lua对象的元表里面:

static void push_collector(lua_State* L, const char* type, lua_CFunction col) {
 
    /* push collector function, but only if it's not NULL, or if there's no
       collector already */
    if (!col) return;
    luaL_getmetatable(L,type);
    lua_pushstring(L,".collector");
        //....
    lua_pushcfunction(L,col);
        //....

 而发生gc的时候,class_gc_event函数会去在lua对象的元表里面找".collector"这个key,如果没找到,就用default的析构,否则就用用户提供的析构函数:

top = lua_gettop(L);
if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */
{
    /*fprintf(stderr, "Found type!\n");*/
    /* get gc function */
    lua_pushliteral(L,".collector");
    lua_rawget(L,-2);           /* stack: gc umt mt collector */
    if (lua_isfunction(L,-1)) {
        /*fprintf(stderr, "Found .collector!\n");*/
    }
    else {
        lua_pop(L,1);
        /*fprintf(stderr, "Using default cleanup\n");*/
        lua_pushcfunction(L,<strong>tolua_default_collect</strong>);//这个是默认的析构函数
    }
 
    lua_pushvalue(L,1);         /* stack: gc umt mt collector u */
    lua_call(L,1,0);


 而默认的析构函数是C free的简单封装:

TOLUA_API int tolua_default_collect (lua_State* tolua_S)
{
 void* self = tolua_tousertype(tolua_S,1,0);
 free(self);
 return 0;
}

如果你通过tolua++注册一个复杂类型给lua的话,析构函数不被调用,而直接调用了free,会发生很多未定义行为.

这就是在我们服务器中隐藏了超过两年的一个Bug......

原文地址:http://www.cnblogs.com/egmkang/archive/2012/07/01/2572064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
tolua++是一个将C/C++代码导出到Lua的工具,它可以将C/C++代码转换成Lua解释器调用的API,并在运行将C/C++代码封装成Lua可用的函数。 以下是一个简单的示例,演示如何使用tolua++将C++代码转换成Lua可调用的库: ```c++ // example.h class Example { public: Example(int value); int getValue(); private: int m_value; }; ``` ```c++ // example.cpp #include "example.h" Example::Example(int value) : m_value(value) { } int Example::getValue() { return m_value; } ``` 首先,我们需要使用tolua++生成绑定代码。假设我们的绑定代码文件名为examplebindings.pkg,其内容如下: ``` // examplebindings.pkg $#include "example.h" module example { class Example { public: Example(int value); int getValue(); }; } ``` 然后,我们可以使用tolua++命令行工具将绑定代码生成为C++头文件和Lua文件: ``` $ tolua++ -o examplebindings.h -H examplebindings.hpp examplebindings.pkg ``` 生成的examplebindings.h和examplebindings.hpp文件包含了C++的绑定代码,我们需要将其与原始的C++代码一起编译成库文件。 接下来,我们可以编写Lua脚本来调用C++代码: ```lua -- example.lua require "examplebindings" local example = example.Example(42) print(example:getValue()) -- 输出42 ``` 在上面的示例,我们首先加载了examplebindings模块,然后创建了一个Example对象,并调用了其getValue()方法。注意,我们使用了冒号语法来调用方法,这是Lua常用的一种语法。 最后,我们可以使用Lua解释器来运行example.lua脚本,以调用C++代码并输出结果。 以上就是使用tolua++将C++代码转换成Lua可调用的库的基本流程。需要注意的是,tolua++只支持部分C++特性,例如模板和多重继承等特性可能无法正确导出。在实际使用,我们需要根据具体情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值