[英]luabind abort when trying to call object method with lua errors
我使用了http://www.rasterbar.com/products/luabind/docs.html#deriving-in-lua中的示例來定義我可以從lua派生的c ++類:
class base
{
public:
base(const char* s)
{ std::cout << s << "\n"; }
virtual void f(int a)
{ std::cout << "f(" << a << ")\n"; }
};
struct base_wrapper : base, luabind::wrap_base
{
base_wrapper(const char* s)
: base(s)
{}
virtual void f(int a)
{
call<void>("f", a);
}
static void default_f(base* ptr, int a)
{
return ptr->base::f(a);
}
};
...
module(L)
[
class_<base, base_wrapper>("base")
.def(constructor<const char*>())
.def("f", &base::f, &base_wrapper::default_f)
];
然后,我在lua中創建了一個派生類:
class 'base_derived' (base)
function base_derived:__init(str)
base.__init(self,str)
end
function base_derived:f()
this_function_doesnt_exist()
end
任何對'f'的調用都應該引發lua錯誤,如果我在lua中這樣做,它會很好地工作:
local x = base_derived("Test")
x:f() -- Throws "attempt to call a nil value..." error
我想做相當於的事情,但是在c ++中:
auto g = luabind::globals(l);
auto r = g["base_derived"];
if(r)
{
luabind::object o = r("Test");
auto gm = luabind::object_cast<base_wrapper*>(o);
if(gm != nullptr)
{
try
{
luabind::call_member<void>(o,"f",5);
}
catch(luabind::error &e)
{
std::cout<<"[LUA] Error: "<<e.what()<<std::endl;
}
}
o.push(l);
}
但是,“ luabind :: call_member”調用會導致“ luabind / detail / call_member.hpp”第258行中止:
// Code snippet of luabind/detail/call_member.hpp
~proxy_member_void_caller()
{
if (m_called) return;
m_called = true;
// don't count the function and self-reference
// since those will be popped by pcall
int top = lua_gettop(L) - 2;
// pcall will pop the function and self reference
// and all the parameters
push_args_from_tuple<1>::apply(L, m_args);
if (pcall(L, boost::tuples::length<Tuple>::value + 1, 0))
{
assert(lua_gettop(L) == top + 1);
#ifndef LUABIND_NO_EXCEPTIONS
////////////////////////////////////////////
throw luabind::error(L); // LINE 258
////////////////////////////////////////////
#else
error_callback_fun e = get_error_callback();
if (e) e(L);
assert(0 && "the lua function threw an error and exceptions are disabled."
"If you want to handle this error use luabind::set_error_callback()");
std::terminate();
#endif
}
// pops the return values from the function
stack_pop pop(L, lua_gettop(L) - top);
}
該行中的異常實際上並未拋出,但這是導致異常終止的原因。
但是,僅當lua函數導致lua錯誤時才會中止。 如果我評論“ this_function_doesnt_exist()”調用,那么lua版本和c ++版本都可以正常運行。
為什么是“ throw luabind :: error(L);” 導致異常終止,即使存在潛在的lua錯誤,我該怎么做才能安全地從c ++調用該函數?
//編輯:這是中止時的調用堆棧(當調用'luabind :: call_member(o,“ f”,5);'時):
> vcruntime140d.dll!__CxxFrameHandler(EHExceptionRecord * pExcept, unsigned __int64 RN, _CONTEXT * pContext, _xDISPATCHER_CONTEXT * pDC) Line 213 C++
ntdll.dll!RtlpExecuteHandlerForException() Unknown
ntdll.dll!RtlDispatchException() Unknown
ntdll.dll!KiUserExceptionDispatch() Unknown
KernelBase.dll!RaiseException() Unknown
vcruntime140d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 136 C++
server.dll!luabind::detail::proxy_member_void_caller<boost::tuples::tuple<int const * __ptr64,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> >::~proxy_member_void_caller<boost::tuples::tuple<int const * __ptr64,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> >() Line 258 C++
參見第254行:
if (pcall(L, boost::tuples::length<Tuple>::value + 1, 0))
在這里, pcall
要求lua執行您的x:f(5)
等效調用。 顯然,此代碼返回錯誤,因為pcall()返回的內容不同於零。 這是預料之中的,因為您確實通過調用this_function_doesnt_exist()
在lua中創建了錯誤。 這也解釋了為什么在注釋this_function_doesnt_exist()
調用時不會發生中止。
然后,轉到C ++代碼:
throw luabind::error(L);
從析構函數~proxy_member_void_caller()
引發此錯誤,事實證明, 從析構函數引發異常是不好的做法 。 這個問題以luabind( 請參閱此問題 )而聞名,它可以觸發甚至不拋出的異常終止調用。
該解決方案是增加noexcept(false)
以簽名~proxy_member_void_caller()
就像這樣:
~proxy_member_void_caller() noexcept(false)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.