lua-users home
lua-l archive

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


So I've further narrowed down the lua/tolua memory-leak that I am 
experiencing.  I've gotten tolua out of the 
equation, and merely replaced it with a fairly straightforwad test-
app that illustrates the same behavior as tolua.

the source-code is below.  It's actually slightly modified from the 
source-code I compiled: I removed
the EPOC-specific mumbo-jumbo that does not affect the memory-leak 
(oh, did I mention I'm using EPOCLua?  I do believe I 
visually verified the same problem is in the most recent main 
release, too).  Anway, the code is:



int gc_tagmethod(lua_State* L)
{

  lua_pushuserdata(L, (void*) 0xbeef);  // leak is a side-effect from 
here!
  lua_pop(L, 1);

  return 0;

}

int gimme_a_userdata(lua_State* L)
{
  int tag = lua_newtag(L);

  lua_pushcfunction(L, gc_tagmethod);
  lua_settagmethod(L, tag, "gc");
  lua_pushusertag(L, (void*) 0xbad, tag);

  return 1;
}


main()

{

  lua_State* L = lua_open(1024);

  lua_register(L, "gimme_a_userdata", gimme_a_userdata);

  lua_dofile(L, "c:\\System\\Apps\\lua\\gimme.lua");

  lua_close(L);

}




and the gimme.lua script is simply:

oFoo = gimme_a_userdata()


This is all it takes to get a memory leak: lua_close() calls the
gc tagmethod on oFoo ( gc_tagmethod() ) which calls lua_pushuserdata
().  This push creates a new userdata object and adds it to the "udt" 
table.  But the udt table has already been cleaned up (collected) 
before gc_tagmethod() is called - and no one goes back and re-clean 
udt (!).

So that's it, in a nutshell.  Anyone have any suggestions?  I really 
hate the idea of added another lua_collectuserdata() to re-clean the 
udt, but don't quite see a way around it, yet, if tolua definitely 
wants to call lua_pushuserdata() during its garbage-collection 
tagmethod.  


Thanks for any help,

-andrew