2

So I have a very simple Lua script like this:

return coroutine.create(function () coroutine.yield(1) end)

And then in C I run it and gets the returned value

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);
}

Later, the C code will pass the co pointer back into Lua (with lua_pushthread) and run coroutine.resume(co).

I would like to know if Lua will GC the coroutine object in the meantime, rendering the co pointer in C invalid? If yes, what can I do to prevent that?

KevinResoL
  • 982
  • 8
  • 19

1 Answers1

1

With a little care, you can just leave the coroutine in the stack. Just remove the call to lua_pop.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • but then later on I couldn't clear it from stack? Because I would have lost track of the stack position of that particular coroutine value? – KevinResoL Aug 17 '18 at 09:17