lua-users home
lua-l archive

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


Thanks Jerome, you seem to be confirming what I had hoped was correct:

In my words, that step 3 resurrects the object and prevents its collection.

In your words, that it was either already collected or became incollectible.

Basically, I should have no problem with the weak references. And for any other problems I might be having, I already have mechanisms to deal with them. So I think I'm good to go.


On Tue, Nov 12, 2013 at 12:11 PM, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
2013/11/12 Marc Lepage <mlepage@antimeta.com>:
> That would be convenient for me. Create a table entry (light to full) when
> full is first created; any subsequent access by light userdata gets the same
> existing full userdata (via that table), and when all references go away,
> the full userdata is "collectible". If the table is weak, it will be cleaned
> up when the full userdata is collected. But in the meantime, any new access
> by light userdata would try to return the same full userdata (which is
> "collectible"). Is this a problem?

The important point here is that there is no "in the meantime". The
userdata is collectible only as long as there is only weak references
to it. When you take your full userdata from the table through the
weak reference, you push it on the stack, and the stack itself is a
strong reference. So either it was collected before you tried to
access it and it's no longer accessible (so your lua_gettable pushed
nil), or it was not and it's no longer collectible.

Weak references work because you cannot do anything with them except
turn them into strong references, at which point you can ignore
anything related to weak references.

> Basically, I'm trying to ensure there can only ever be one full userdata per
> particular C++ object. (Because that would simplify a lot.)

Weak references are perfect for this case.