Reputation: 21
Usually, I leave the Lua stacky messy with up to 6 remaining entries after a function I defined has been run.
In some cases, this has caused severe memory leaks / stack overflows! I solved them by using lua_settop( L, 0 ) at the end of my function.
In other cases, there were no leaks even though the stack was not empty at the end of my function.
Upvotes: 2
Views: 2025
Reputation: 3179
void lua_pop (lua_State *L, int n);
/*Pops n elements from the stack.*/
As for why - cant really tell - there are many possible reasons, could you post some examples of the code that caused this? A few common (imho) reasons below.
Lua should not cause any memory leaks, unless you used light userdata (the only one that is not managed by garbage collector).
Did you malloc anything instead of using lua_newuserdata?
Have you checked the stack size at the start of the function? Maybe you just passed that many parameters from Lua (for example calling c_func(unpack(huge_table)); )?
Upvotes: 0
Reputation: 72312
You do not need to clean the Lua stack when exiting a Lua function written in C, provided that function is called from Lua, of course. If you are experiencing memory leaks, please post a minimal example that shows the problem. If the stack has 10000 entries, make sure you have called lua_checkstack or luaL_checkstack.
Upvotes: 3