lua-users home
lua-l archive

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


Jim Mathies wrote:
> 
> I'm having a problem where Lua 3.2 is garbage collecting a userdata
> variable even though the userdata value is stored in a global table.
>[...] 
> tag = lua_newtag()
> lua_pushcfunction( cpointer_gc_function );
> lua_settagmethod( tag, "gc" );
> lua_pushusertag( (void*)cpointer, tag );

Ok up to here.

> lua_pop();

You remove your tagged userdata here!??

> lua_pushobject( table );
> lua_pushnumber( index );  ----> index = 1
> lua_pushuserdata( cpointer );

and create a new one with the default tag???

> lua_rawsettable();

and put this one into the table!

>[...]  Since 'mytable' still has the cpointer userdata, I don't understand
> why Lua is calling gc.

mytable has a cpointer userdata with the default tag.  Your userdata
with the private tag was never stored in the table.  So the GC is free
to collect it...

Ciao, ET.