我目前在使用 Luabind 将 Lua 脚本 AI 与 C++ 游戏交互时遇到问题。
我在一个循环中调用一个更新函数(每帧一次),这个函数从 Luabind 中注册的 C++ 函数中检索信息。
我的问题如下: 在可变的、不可预测的时间之后,Luabind 中出现断言失败,导致中止。 错误总是在/usr/include/luabind/wrapper_base.hpp:124 中下降时发生 Lua.
您知道什么可以做到这一点吗? 对于我的测试,C++ 和 LUA 中调用的函数总是相同的。
关于问题的更多细节:
wrapper_base.hpp 中失败断言的内容
typedef typename boost::mpl::if_<boost::is_void<R>, luabind::detail::proxy_member_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
, luabind::detail::proxy_member_caller<R, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type proxy_type;
// Comments removed
lua_State* L = m_self.state();
m_self.get(L);
assert(!lua_isnil(L, -1));
detail::do_call_member_selection(L, name);
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
throw std::runtime_error("Attempt to call nonexistent function");
}
// push the self reference as the first parameter
m_self.get(L);
// now the function and self objects
// are on the stack. These will both
// be popped by pcall
return proxy_type(L, args);
确切的错误
bomberman: /usr/include/luabind/wrapper_base.hpp:124: typename boost::mpl::if_<boost::is_void<T>, luabind::detail::proxy_member_void_caller<boost::tuples::tuple<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> >, luabind::detail::proxy_member_caller<R, boost::tuples::tuple<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> > >::type luabind::wrap_base::call(const char*, luabind::detail::type_<Derived>*) const [with R = void]:
Assertion `!(lua_type(L, (-1)) == 0)' failed.
Aborted (core dumped)
最佳答案
几天前我遇到了这个问题。在我的特殊情况下,隐式创建的 Lua 表保存了在 Lua 中为每个对象覆盖的所有方法,正在被垃圾收集,而底层 C++ 对象则没有。因此,如果您尝试从 C++ 对象调用 Lua 成员函数,它将失败。
在我的案例中,解决方案是只要 C++ 实例处于事件状态,就保持对 lua 表的引用处于事件状态。这就像在 C++ 类中添加一个 luabind::object 字段然后在实例化类时设置它一样简单,当 C++ 类的析构函数被调用时它将被销毁,所以在大多数情况下你不必担心内存泄漏。我的代码现在看起来像这样:
class LuaC : public BaseC, public luabind::wrap_base {
private:
luabind::object _self; //retain a reference to the Lua part of this object so it doesn't get gc'd
public:
void setSelf(luabind::object nSelf) { _self=nSelf; }
};
//expose LuaC including the method setSelf
// ...
(BaseC 是您要包装的 C++ 类)
然后在 Lua 中,无论何时实例化 LuaC 的实例,调用 setSelf 并将 self 作为附加参数传递
c = LuaC()
c:setSelf(self)
如果没有一种方法可以简化它并将其全部放入 LuaC 构造函数中以减少出错(即您不必担心每次都调用 setSelf),我会感到惊讶。但是 LuaBind 的文档相当浅,所以我找不到任何方法来做到这一点。
此外,我认为出现此问题的唯一方法是,如果您告诉 Luabind 仅使用像 shared_ptr
这样的引用,因为这样 Lua 部分将与 shared_ptr 一起被垃圾收集,但不一定是指针引用的 C++ 实例。如果 Lua 正在管理整个实例,那么我看不出在 C++ 实例继续存在时如何删除 Lua 表。
关于c++ - Luabind 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6236827/