lua-users home
lua-l archive

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

2013/11/12 Marc Lepage <>:
> 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.