lua-users home
lua-l archive

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


> -------- Original Message --------
> Date: Mon, 18 Oct 2010 22:56:24 +0200
> From: Francesco Abbate <francesco.bbt@gmail.com>
> Subject: Re: Userdata finalization order
> To: Lua mailing list <lua-l@lists.lua.org>
> Message-ID:
> 	<AANLkTi=wVCcshiGV1KDq3gG5P9jdn01udJh58RjCtpTt@mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> 2010/10/18 Javier Guerra Giraldez <javier@guerrag.com>:
> > i can't think of an example where:
> >
> > - A can be created before B
> > - but A depends on B  (how did it exist before B, then?)
> > - you have to destroy A before B
> 
> For example A is a plot and B is a line or a graphical elements. You
> create first the plot wichh is initially empty and then you add
> graphical elements like lines or circles or something else. Once you
> have added an element to the plot this latter store a *reference* to
> the element and it is therefore important to ensure that B (the
> element) is not collected as far as A (the plot) exists.
> 
< ... SNIP ... >
> 
> -- 
> Francesco

By your description, it sounds like an "owner" pattern -- plot A owns
line B, and on the C++ side, B is deleted when A destructs, i.e. B would
not be deleted independently from A if you were writing the app in C++.

So, couldn't this problem be avoided by *not* having a finalizer for
line B?

i.e. the only way line B would be C++ destructed is when plot A's
destructor is called and it deletes all contained elements (one of which
is line B).

This would work on final cleanup and also if plot A were dereferenced
prior to final cleanup. You would still need to ensure no Lua code
dereferences line B independent of plot A.

John Giors
Independent Programmer
Three Eyes Software
jgiors@ThreeEyesSoftware.com
http://ThreeEyesSoftware.com