Lua 4.01 Garbage collection and tags

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Lua 4.01 Garbage collection and tags

D Burgess
I have an obvious misunderstanding of Lua 4 gc events.
If someone tell me what I am doing wrong I would
greatly appreciate it.

The problem I have is that my userdata is not collected
until Lua terminates. The gc event is not called until
termination but is called as I would expect at termination.

The C code is one Lua function that returns a table
containing other functions.

the Lua code looks like:

atable = myfnc();
atable = nil;
collectgarbage(); -- would expect gc event to be called here


The C code looks like:

typedef struct _iodata {
  int rtag;
  int invaltag;
} io_t; 

static int my_gcmethod(lua_State *L)
  io_t *rdata = (io_t*) lua_touserdata(L, -1); /* gets data from closure value */
/* only gets here at VM termination */
  return 0;

static int fnc(lua_State *L)
  io_t* rdata = (io_t*)lua_newuserdata(L, sizeof(io_t));
  if (!rdata) return fail(L, "recio: allocation error.");

  rdata->rtag     = lua_newtag(L);
  rdata->invaltag = lua_newtag(L);

  lua_settag(L, rdata->rtag);
  lua_pop(L, 1);

  lua_pushusertag(L, (void *)rdata, rdata->rtag);
  lua_pushcclosure(L, my_gcmethod, 1);
  lua_settagmethod(L, rdata->rtag, "gc");


/* functions with rdata as the closure */
  lua_pushstring(L, "afunc");
  lua_pushusertag(L, (void *)rdata, rdata->rtag);
  lua_pushcclosure(L, afunc, 1);
  lua_settable(L, -3);
  return 1; /* return the table */
David Burgess
[hidden email]