【发布时间】:2010-10-23 20:14:36
【问题描述】:
我最近偶然发现了这个 C++/Lua 错误
int function_for_lua( lua_State* L )
{
std::string s("Trouble coming!");
/* ... */
return luaL_error(L,"something went wrong");
}
错误是luaL_error使用longjmp,所以堆栈永远不会展开,s永远不会被破坏,内存泄漏。还有一些 Lua API 无法展开堆栈。
一个明显的解决方案是在 C++ 模式下编译 Lua,但有异常。但是,我不能,因为 Luabind 需要标准的 C ABI。
我目前的想法是编写自己的函数来模仿 Lua API 的麻烦部分:
// just a heads up this is valid c++. It's called a function try/catch.
int function_for_lua( lua_State* L )
try
{
/* code that may throw Lua_error */
}
catch( Lua_error& e )
{
luaL_error(L,e.what());
}
所以我的问题是:function_for_lua 的堆栈是否正确展开。会出什么问题吗?
【问题讨论】:
-
当你提到 Luabind 时,我很困惑。 Luabind 本身是一个 C++ 库,但是看起来你怎么也没有使用它?
标签: c++ lua destructor stack-unwinding