lua-users home
lua-l archive

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




On 19/11/15 03:02 PM, Thiago Padilha wrote:
> If I understand what you're saying; Lua is blind to the fact that B has a reference to A

Yes

> that should prevent A from being garbage collected

I still want the __gc metamethod associated with "A" to be called so I know when there are no references to it other than "B", but I need to stop lua from freeing "A" memory until "B" has completely removed "A" reference(Assume that "B" is a collection of objects of type "A")

> You could create a table entry somewhere that has B as a (weak) key and A as the value. In function "C", remove the table entry. So long as B exists then so does A, because of the table entry you created. If either C is called or B itself is garbage collected, then the table row is removed and A becomes eligible for garbage collection.

I'm more interested in deferring "A" garbage collection. For this problem, we can even assume that "B" is never garbage collected.
Use a closure for "C". Have "A" as an upvalue. When "C" is called, clear that upvalue (set it to nil).

On Thu, Nov 19, 2015 at 1:46 PM Peter Pimley <peter.pimley@gmail.com <mailto:peter.pimley@gmail.com>> wrote:

    If I understand what you're saying; Lua is blind to the fact that
    B has a reference to A that should prevent A from being garbage
    collected.

    You could create a table entry somewhere that has B as a (weak)
key and A as the value. In function "C", remove the table entry. So long as B exists then so does A, because of the table entry you
    created.  If either C is called or B itself is garbage collected,
    then the table row is removed and A becomes eligible for garbage
    collection.

    On 19 November 2015 at 14:28, Thiago Padilha <tpadilha84@gmail.com
    <mailto:tpadilha84@gmail.com>> wrote:

        Consider the following scenario:

        I have a userdata "A" being referenced referenced by another
        userdata "B". "A" has the __gc metamethod that can be used to
        schedule its removal from "B", but note that the reference is
        not removed immediately, it is deferred until a certain
        function "C" is called(This is how the library I'm working
        with behaves). The problem is that if Lua frees "A" memory
        before "C" is called, "B" will be left with a dangling pointer
        that can cause memory errors when "C" is called later(It will
        be called for sure).

        What I want to achieve is to defer "A" memory from being freed
        until C is called. Is there a way to do this? I know that one
        option is to work with userdata that simply references "A" and
        take care of freeing memory myself, but I'd rather avoid this
        because it would need a lot of refactoring in the project I'm
        working on.



--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.