gpt4 book ai didi

c - Lua 对象 "globally returned"到 C 的生命周期

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:54 25 4
gpt4 key购买 nike

所以我有一个像这样的非常简单的 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指针失效?如果是,我可以做些什么来防止这种情况发生?

最佳答案

稍加小心,您可以将协程留在堆栈中。只需删除对 lua_pop 的调用即可。

关于c - Lua 对象 "globally returned"到 C 的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48679339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com