lua-users home
lua-l archive

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



On 14-Oct-05, at 8:57 AM, Jan Kratochvíl wrote:

int main (int argc, char *argv[]) {
   lua_State *l = lua_open();
   lua_State *lth;
   const char * a = "local a = 'a'"; //just something

   lua_setgcthreshold(l,0);
   printf("gccount before: %d\n",lua_getgccount(l));

   l2 = lua_newthread(l);

This was supposed to be lth = lua_newthread(l), right?

lua_newthread() creates a new thread and pushes the thread object onto the stack. So that is how the garbage collector finds it: as long as it stays on the stack, it is not collectable. Since you never pop the stack, it will not be collected.

If you need a thread to stick around, you need to either leave the thread object on the stack (which is not usually practical) or store it somewhere where Lua can find it (i.e. put it in a table, or in the registry, or some such.) The fact that you have a lua_State* pointer is of no use to the garbage collector; Lua is quite capable of garbage collecting the thread anyway, leaving you with a dangling pointer.

This is the one case in which Lua exports an internal pointer as part of its official API, and it is also the case which seems to create the largest number of problems with gc.

R.


   luaL_loadbuffer(lth,a,strlen(a), "TEST");
   lua_resume(lth,0);

   lua_setgcthreshold(l,0);
   printf("gccount after: %d\n",lua_getgccount(l));

   lua_close(l);
   return 0;
}