lua-users home
lua-l archive

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


Hi all,

it seems that I've discovered a bug in Lua 5.1.4 related to the
garbage collection. Of course it is possible that I'm wrong, till now
I've never found a single bug in Lua and I know in general that its
code quality is excellent but it seems that I've found a bug.

So, let me explain the problem: I've two userdatas, the first of type
"plot" and the second of type "line". In the userdata "plot" I set up
a custom "f environment" with setfenv in order to keep some references
to other Lua objects.

I can add a "line" object in a "plot" object and what happens is that:
- the C++ class underlying to "plot" will store a pointer to the C++
object underlying
  the "line" object
- in the the fenv of "plot" I will store a reference to the Lua object
of type "line"

In this way I make sure that the object "line" will not be disposed if
there is around a "plot" object that make a reference to it.

The problem happens when the plot is no more referenced. In this case
what can happen is that the "line" object referenced by the plot is
disposed *before* the "plot" object. This cause a segmentation fault
because when, in turn, we proceed to deallocate the "plot" object we
found a pointer to a corrupted "line" object.

My expectations was that the "line" object get disposed only *after*
the plot object because the plot make reference to it.

I hope someone can help me with this bug or otherwise explain me what
I'm doing wrong.

Best regards,
Francesco