lua-users home
lua-l archive

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


Le 20 oct. 2010 à 09:17, Francesco Abbate a écrit :
> --------------
> a = fxline(sin, 0, 2*pi) -- create a line
> b = plot('Test plot')
> -- add the line a to plot b as a dashed line
> b:addline(a, 'red', {{'dash', 7,3,3,3}})
> b:show()
> --------------
> 
> What's wrong about that ?
> 

Nothing but you have to tell the GC that "b" keep a reference on "a". You can do this a lot of way.

A simple one where you will see the "reference" explicitly is to do a <luaL_ref> in the addline method and keep in "b" the returned int, this will tell the GC that you are keeping a reference on this object. Next in the __gc of "b" you just do a <luaL_unref> with the kept int to tell the GC that you release this reference.

This will make your code OK, as the reference is not stored in the object itself but in the registry, so "a" and "b" will not be collected in the same cycle and so the order will be always respected.