lua-users home
lua-l archive

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


Alex Ames wrote:
> So this code:
> > Button* self = (Button*)lua_touserdata(L, 1);
> 
> becomes this:
> > Button* self = *(Button**)lua_touserdata(L, 1);
> 
Sorry, this was a misprint. I am actually storing pointers, as you
suggest (i.e. the second way).

> You then keep a reference count of how many
> userdatas represent your object. You can do the reference counting by
> using the object itself as a lightuserdata as the key into a table
> containing the reference counts. When the __gc metamethod is called
> and frees the last reference to an object, you can delete it and
> free/delete/nil any data it was using.

Yes, that's already implemented (I just omitted the details in my
description of the problem to keep it short). But it doesn't solve the
problem with the circular reference. That is, in a code like

local b = Button()
b:on_click(function() b:some_method() end)
b = nil

the __gc metamethod for `b' will be never called and the object will
never be deleted, thus he have a memory leak (until the interpreter is
destroyed).