lua-users home
lua-l archive

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

Thank you, so only one miracuolous line more after each reset_thread:
lua_pop( LuaBase, 1)

... and now really thread world looks nice so far... thank you... (i tested it, it seems to work perfect now as far as I can see it... at least LuaBase->top keeps stable now, also for 10000 such thread invocations and killings... and my STM32G4 ARM-CortexM4 system with quite limited stack and heap for sure would pinch me in case of ANY problems) .

On Mon, Jun 14, 2021 at 9:35 AM Francisco Olarte <> wrote:

On Sun, Jun 13, 2021 at 10:16 PM Flyer31 Test <> 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.