lua-users home
lua-l archive

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


2010/10/19 Wesley Smith <wesley.hoke@gmail.com>:
> I think the problem you're running in to as I said before is that
> there is no clear line of ownership in the C++ classes, so the user
> has to manage it.  If that's the case, you can make an abstraction
> layer in C++ that does this without involving Lua (this is the idea of
> the factory in my previous email), implement something like reference
> counting that that the userdata knows when it's safe to free itself,
> or push the responsibility on to the Lua scripter, which is probably
> not so nice since the user will have to manually untangle objets that
> depend on each other.

Hi Wesley,

what you are saying is ok, really. You can use reference counting and
it will work, you will just implement an additional mechanism over Lua
GC because you recognize that Lua itself is not able to do it.

Otherwise you are talking about a "clear line of ownership" but if you
look at the following simple Lua code:

-------------
a = {}
b = { "I'm an object" }
a.content = b

function print_content (x)
  print("Content:", x.content)
end
-------------

you will recognize the this code is pretty simple and there is nothing
wrong about it. But, wait... we didn't define a clear line of
ownership!!

Is "a" the owner of its content ? Can several objects share the same
content "b" ? and who is the owner in this case ? what a mess... we
are going to have a lot of trouble with this code because there is
really no clear line of ownership!

May be you can recognize that there is no problem with the code above
because you have the GC, what a wonderful thing!

Now understand that what I want to do is to make Lua's GC work also
with the following code:

--------------
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 ?

You will remark that the code is, from the logical point of view, the
same of the code above with simple native Lua objects. What I was
willing to do is to make the Lua GC do the work *normally*. The
problem is that it can't because the GC is not able to determine the
the userdata "b" (the plot) depends on the object "a" (the line,
another userdata). This is why the people are using either:
- reference counting
- weak tables

Clear enough ?

Now, what I'm trying to say is: why we cannot add code to Lua core to
make the GC aware that the userdata "b" depends (refers) to the
userdata "a" so that the GC can make his job correctly without
additional mechanisms ?

This is the question that I'm presenting to tha mailing list since
some time without any clear answer or an explication of the rationale
of why we don't need such feature.

I hope now my point is more clear.

-- 
Francesco