叠使用的Lua当在C ++中退绕
-
25-09-2019 - |
题
我最近偶然进入这个这个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的。
一个明显的解决方案是编译的Lua在C ++模式有例外。 I,但是,不能作为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
函数抛出异常的正确捕捉和翻译反正。 (请参见参考。)
所以每当你需要指出一个错误,只是抛出一个标准的异常:
void function_for_lua( lua_State* L )
{
std::string s("Trouble coming!");
/* ... */
// translated into lua error
throw std::runtime_error("something went wrong");
}
<子> 免责声明:我从来没有使用Lubind。 子>
不隶属于 StackOverflow