lua-users home
lua-l archive

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

 > On 1/8/08, Norman Ramsey <> wrote:
 > > The only way I can think of to do this is to use a weak table
 > > in which both keys and values are weak and the light userdata is the key,
 > > the heavy userdata is the value.  Is this the correct approach?
 > > Are there other possible approaches?
 > i guess it should be weak keys and strong values

OK, I'm still trying to figure out why.  The explanation of weak
tables in section 2.10.2 is not precise enough for me to understand.
Mabye somebody can tell me if the following description is correct?

  1. If a value v appears as a key in a table with weak keys or as a
     value in a table with weak values, we call those appearances
     'weak references'.

  2. All other appearances (as a key or value in a table without the
     weakness property listed above, as a local variable of a live
     chunk, as a formal parameter of a live function, or as a value on
     the Lua stack) are 'strong references'.

  3. If there are no strong references to a value, it can be

  4. When a value v is garbage collected, if it appears as a key or a
     value in a table, the relevent key-value pair is removed from the

So in the scenario I imagine, I have a light userdata L and a heavy
userdata H.  I want to guarantee

  If there is a strong reference to L, there is a strong reference to H.

So now I think I understand why I want a table with with weak keys L
and strong values H.  If there is a strong reference to L, H stays
alive.  If not, L is collected and in the process the strong reference
to H disappears.

Thanks for letting me think out loud, everybody.