[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Weak tables, user data, and GC
- From: Mark Hamburg <mhamburg@...>
- Date: Mon, 09 Feb 2004 08:21:07 -0800
on 2/9/04 7:06 AM, Roberto Ierusalimschy at roberto@inf.puc-rio.br wrote:
> It is not uncommon to use a weaktable to associate attributes to
> userdata (userdata is the key, attribute is the value). In such uses, it
> is also not uncommon that the finalizer code needs those attributes. So
> Lua does not remove those keys before calling the finalizer. We assume
> that this should not cause problems, because the only way to access
> that pair would be using the userdata as a key, but as the program does
> not have access to that userdata anymore, it could not access that
> entry. (Of course, that is not true if you traverse the table. But
> traversing a weak table is a tricky task anyway...)
I realized that afterward and considered myself slapped for iterating a weak
table. My fix was to maintain a second table mapping the key objects to
themselves. The value side of that table will be nil'd on the first GC pass,
so one can use this to detect finalized userdata without adding an explicit
"is_finalized" operation to the userdata.
Mark