lua-users home
lua-l archive

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


On Tue, Apr 17, 2007 at 05:28:29PM -0400, Brian Weed wrote:
> So, I have this problem with my C++ object binding system.  I've 
> recently added code to store the Lua table that represents my C++ object 
> in the lua registry, with the objects pointer as a key.  I did this so 
> that I can look up the table from the pointer for C++ to Lua callbacks 
> (i.e. to call a buttons OnClick() method from C++).  However, since I'm 
> storing the lua table in the registry, it is no longer garbage collected 
> (since it still has a reference)...I had put the code to remove the 
> reference in the GC method of the object, so of course it never gets 
> called (DOH).

Consider creating a ptr->object mapping table in the registry that has
weak values (the tables), and whose keys are lightuserdata of the C++
object's pointer. Technique described here:

  http://www.lua.org/pil/28.5.html

  http://www.lua.org/pil/17.html

Btw, it sounds a little bit like you may have an __gc metamethod defined
for your tables. Is this case? __gc metamethods aren't called when
tables are garbage collected, they are only called for userdata.
Using userdata to represent objects might have some benefits to you.

Cheers,
Sam