luabind在尝试使用lua错误调用object方法时中止

时间:2015-10-08 19:26:30

标签: c++ exception lua abort luabind

我使用http://www.rasterbar.com/products/luabind/docs.html#deriving-in-lua中的示例来定义c ++中的一个类,我可以从lua中派生出来:

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

打电话给&#39; f&#39;应该抛出一个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);
}

然而,&#39; luabind :: call_member&#39; -call导致&#39; luabind / detail / call_member.hpp&#39;,第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错误时才会发生中止。如果我评论&#39; this_function_doesnt_exist()&#39; -call,那么lua和c ++版本都运行得很好。

为什么&#39;抛出luabind :: error(L);&#39;导致中止,即使有潜在的lua错误,我还能做些什么来安全地从c ++调用函数?

//编辑: 这是中止时的调用堆栈(当&#39; luabind :: call_member(o,&#34; f&#34;,5);&#39;被调用时):

>   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++

这是中止消息: enter image description here

1 个答案:

答案 0 :(得分:2)

见第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(),结果是throwing an exception from a destructor is bad practice。 luabind(see this question)已知这个问题,即使没有投掷就会触发中止调用。

解决方案是将noexcept(false)添加到~proxy_member_void_caller()的签名中,如下所示:

~proxy_member_void_caller() noexcept(false)