Reputation: 97
Although I fixed the bug by call as less time lua_getglobal
as I can, it's not good enough for long term. So does anyone know how do I clean everything in lua's stack to prevent memory management problems?
---EDIT---
From the experiments i just did, lua_settop
will clean up the table to the given value. However, in the time i know how many items are there i want to remove, is lua_pop
more efficient?
Upvotes: 2
Views: 4900
Reputation: 20838
A simple lua_settop(L, 0);
should do the trick.
A simple, albeit contrive, example say you have a lua_CFunction
:
int foo(lua_State *L)
{
// marshal some random data
int bar = luaL_checknumber(L, 1);
const char *baz = luaL_checkstring(L, 2);
// do foo's task
// completely clear the stack before return
lua_settop(L, 0);
return 0;
}
This is contrived because if foo
is called by the VM then cleanup is not necessary. But if you have C++ code calling foo
directly this may be necessary. At any rate hopefully this illustrates its calling context.
Upvotes: 5