lua-users home
lua-l archive

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


On Mon, Aug 6, 2012 at 11:32 AM, Atilim Cetin <atilim.cetin@gmail.com> wrote:
> On Mon, Aug 6, 2012 at 7:58 PM, Coda Highland <chighland@gmail.com> wrote:
>> On Mon, Aug 6, 2012 at 9:55 AM, Atilim Cetin <atilim.cetin@gmail.com> wrote:
>>> Hi,
>>>
>>> When I call collectgarbage in a __gc metamethod, Lua GC tries to collect
>>> another userdata and therefore calls another __gc metamethod before
>>> finishing the current one. And destructing two objects simultaneously
>>> results in crashing. I'm using Lua 5.1.4.
>>>
>>> Do you know a way to avoid this situation?
>>>
>>> Thank you.
>>>
>>> --
>>> Atilim Cetin
>>>
>>
>> "Doctor, it hurts when I do this."
>>
>> Don't call collectgarbage in a __gc metamethod?
>>
>> I mean, I don't know what the technical details involved here are, but
>> this sounds intuitively like a big no-no. What are you trying to
>> accomplish, anyway?
>>
>> /s/ Adam
>>
>
> I've just met this interesting behavior while writing my Box2d
> wrapper. I don't know if it's a bug or not but Lua 5.1.4 avoids GC
> steps while running a __gc metamethod.

(Please try to avoid top-posting. List etiquette says you should add
your new material to the bottom of the mail.)

Without digging into the source code, I'll say it sounds completely
intentional to me. I know that objects that are released during a GC
sweep don't get cleaned up until the following GC sweep. (It's a bad
idea to modify those data structures while you're iterating over them,
you know?) I also know that objects with finalizers don't get
collected until the sweep AFTER the finalizer is invoked. (There's
some trickery available for "resurrecting" an object from within its
finalizer, although the finalizer will only ever get called once.)

My intuition says the appropriate response would be to leave a message
for your mainloop to go and immediately run another sweep next time it
gets control, although I'm not certain that would actually be wise.

/s/ Adam