【发布时间】:2018-07-18 15:50:37
【问题描述】:
所以我有一个非常简单的 Lua 脚本,如下所示:
return coroutine.create(function () coroutine.yield(1) end)
然后在 C 中运行它并获取返回值
lua_State* l = luaL_newstate();
if(luaL_dostring(l, script) == LUA_OK) {
lua_State* co = lua_tothread(l, lua_gettop(l));
lua_pop(l, 1);
}
稍后,C 代码会将co 指针传回Lua(使用lua_pushthread)并运行coroutine.resume(co)。
我想知道 Lua 是否会在此期间 GC 协程对象,导致 C 中的 co 指针无效?如果是,我能做些什么来防止这种情况发生?
【问题讨论】:
-
您应该将此值保存在注册表中以防止 GC 吃掉它。 See also