lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi:

On Sun, Jun 13, 2021 at 10:16 PM Flyer31 Test <flyer31@googlemail.com> wrote:
> I would like to open a new thread, then invoke a Lua-function and then kill the thread again.
> But after some invocations of this, the invocation of api_incr_top(L) in lua_newthread runs into assert concerning some stack error.
> The the lua_State pointer element "top" increases every time quite a bit.

Because you are not cleaning up, look at the stack balance annotations
in the manual.

> lua_State* LuaBase= luaL_newState();
> luaL_dofile( LuaBase, "test.lua");
> for( int i= 0; i< 100; i++){
>   lua_State* L= lua_newthread( LuaBase);

This push the new thread as a lua object in LuaBase stack:

lua_State *lua_newthread (lua_State *L);

Creates a new thread, pushes it on the stack, and returns a pointer to
a lua_State


>   lua_resetthread( L);

This resets it, but does not clean up LuaBase ( and it can not, it
hasn't got a ref to it )

> }

> If I watch LuaBase->top in this for loop, it will increase and increase. Is there some recommended way to avoid this? (thus to somehow "remove the new thread" again completely?).

Try cleaning up properly, pop LuaBase before/after the reset. Normally
anything which makes the stack grow in a loop indicates an stack
imbalance, this is easy, in more complicated ones just look of what is
left to debug it.

Francisco Olarte.