lua-users home
lua-l archive

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


Gaspard Bucher wrote:

> The garbage collection is:
> 
> 
> Lua ---> userdata ---> fenv ---> thread --> function
> 
> 
> Since the function is not stored in a global table (as you do
> with luaL_ref), gc management is easy.
>  

This sounds reasonable but is still not an acceptable solution for me.
The problem is that my C++ objects are internally reference-counted and
can hold references on each other. For example, a container UI widget
holds references on its children. Consider the following code:

-- Get a container widget that is already created in C++:
local container = get_container()

-- Create a new button:
local button = Button()

-- Set the callback:
button:on_click(function() button:some_method() end)

-- Pack the button into the container.
-- Container adds a reference on the button internally.
container:add_child(button)

-- Forget about the button
button = nil

-- The callback will be collected now, but it shouldn't!
-- Button's userdata will be collected too, but its C++
-- object will still be alive.
collectgarbage("collect")


-- Gregory Bonik