lua-users home
lua-l archive

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


Gregory Bonik <gregory@bonik.org> wrote:

> b:on_click(function() b:some_method() end)
> b = nil
> 
> The problem is that an anonymous function passed to on_click() has an
> upvalue which refers to the button, which implies that the button's __gc
> method will not be called while the function exists. But the function is
> referenced in the "refs" table and will not vanish until the destruction
> of C++ object. Hence we get a circular reference which causes a memory
> leak.
> 
> Is there a solution to this problem?

Can you break the dependency by making on_click pass the object to the
handler?

  b:on_click(function(b) b:some_method() end)

-uwe