lua-users home
lua-l archive

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

I just read through this thread.

What I'm doing, and what I'm worried about, is precisely this:

"For example, our proxy system for Objective-C objects uses a weak table mapping light userdata Objective-C object addresses to Lua proxies so that we generate the same proxy for a userdata object if we need to publish it to Lua multiple times. It would be really bad if we could end up using a proxy that had been scheduled for finalization."

And it sounds like without "indirect" references, my "direct" references will be OK:

"Direct caches of userdata get cleared when marked for finalization. It's just indirect caches that have problems and those need something else to keep the values alive."

I was also worried about "It can happen so that the new view is allocated exactly at the same address as the old one" but as in the thread, if my userdata isn't finalized, the C++ object remains so nothing else can be allocated there. I do have another case where I can have auxillary data cached to the light userdata, without a full userdata keeping it alive, and I already intended to hook in a callback to the object's destructor to clear any such auxillary data. That callback would only be active if such auxillary data existed.

Still, reading through that thread, it *seems* to me like I should be OK, but it's not *obvious* to me that it's OK.

On Tue, Nov 12, 2013 at 10:24 AM, Marc Lepage <> wrote:
Hi, I'm not sure if the manual or wiki talked about this, if so point me to the section and I'll be glad to read up on it again.

If I have some Lua references to a full userdata, and elsewhere I have a weak table of light userdata to full userdata so I can look them up.

At some point, I may lose all my strong references to the full userdata in Lua. So the object becomes "collectible". But I'm guessing that it might not be collected at that very moment? And the weak reference won't go away right away?

So the situation I want to avoid is this:

1) You have Lua references to full userdata (and weak references in the table)
2) The Lua references go away (but the weak reference in the table remains)
3) Later in Lua I try to get access to the light userdata and reuse the full userdata because it remains in the table as a weak reference
4) And that full userdata is still scheduled for collection

Because step 3 occurs in C code: I am asking for a Lua reference to a full userdata representing a C object, and it will check the table first to return an existing one (if it exists).

Or, does doing step 3 prevent the collection from occurring, by resurrecting the object (making new references to it)?