lua-users home
lua-l archive

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


One other issue with finalizers to be aware of is that if you store a
userdata object as the key in a weak-keyed table (e.g., to track a set of
objects), it will remain in the table after being finalized and won't get
removed until it is actually collected.

The standard answer with respect to this situation is "Don't iterate
weak-keyed tables", but there are times when one really wants to do so.

My other approach if the table represents a set is to store set entries not
via:

    set[ element ] = true

But via:

    set[ element ] = element or true

You need to then be sure that the set is a fully-weak table. What this
relies on is that while keys won't be removed immediately from weak tables,
values will be.

If the table in question isn't a set, then it can be useful to store a weak
set of valid userdata values alongside the main weak table.

Mark