“Lua对象的生存期”;“全球返回”;到C

“Lua对象的生存期”;“全球返回”;到C,c,lua,lua-api,C,Lua,Lua Api,我有一个非常简单的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脚本,如下所示:

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\u pushthread
)并运行
coroutine.resume(co)


我想知道Lua是否会同时GC coroutine对象,使C中的
co
指针无效?如果是,我可以做些什么来防止这种情况发生?

只要稍微小心,您就可以将协同路由留在堆栈中。只需删除对
lua\u pop

的调用,您应该将此值保存在注册表中,以防止GC吃掉它。但后来我无法从堆栈中清除它?因为我会忘记那个特定的协程值的堆栈位置?