lua-users home
lua-l archive

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


2010/10/19  <jgiors@threeeyessoftware.com>:
> 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.

Well, this is quite not true because the object B can exists also
indipendently from object A. For example you can write in GSL shell
the following code:

ln = fxline(sin, 0, 2*pi) -- create a "line" object
p1 = plot('Test plot #1') -- create a "plot"
p1:addline(ln)
p2 = plot('Test plot #1')
p2:addline(ln)

As you can see the object "ln" can exists indipendently from any plot
but it can also be attached to one or many plot (this is the zero,
one, infinity principle, I guess :-) )

So neither "p1" or "p2" owns the object line. The object line exists on its own.

Note that this pattern is perferctly normal for Lua and if all the
objects were native Lua objects there would be absolutely no problem.
The GC knows about the existing objects and know that p1 refers to
"ln" so that it will never deallocate "ln" as far as either p1 or p2
are alive.

The troubles came when ln, p1, p2 and C++ objects since, as I'm trying
to explain, Lua does not have any satisfying mechanism to ensure
proper management of the reference among C++ objects. The only model
that works is to create a reference counted objects, then create the
userdata which is just as a boxed C++ pointer and define the Lua
finalizer function so that it just "unref" the object. In this way the
object will be freed only when nobody make reference to it and
everyone will be happy.

--
Francesco