lua-users home
lua-l archive

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


I have some questions on userdata in Lua.


1) malloc vs newuserdata
struct data_t ...

data=malloc(n)
data->tag  = lua_newtag(L);
lua_pushusertag(L, (void *)data, data->tag);
lua_pushcclosure(L, data_gcmethod, 1);
lua_settagmethod(L, data->tag, "gc");

versus

data = lua_newuserdata(L, sizeof(data_t));
data->tg = lua_newtag(L);
lua_pushusertag(L, f, data->tag);
lua_pushcclosure(L, data_gcmethod, 1);
lua_settagmethod(L, data->tag, "gc");

a) Am I right in presuming that the only difference is that Lua
will clean up the "data" with newuserdata, but if I malloc
I need to do it in the "gc" method?

b) Does the lua_close() unconditionally call the garbage collection
process and hence clean up all allocated memory?

c) Which is the preferred way of doing this? The IOLIB uses newuserdata
and Lua/SQL uses malloc().

2) Does Lua cleanup memory from newuserdata() if I dont have a 
gc method()?

3) How do I stop Lua from freeing a userdata?

4) What does a tag value of zero imply?

David Burgess