lua-users home
lua-l archive

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


> Does it have something to do with the way weak tables work ? Is it
> normal that the entry is silently removed from my table at some point
> without my __gc metamethod being called ?

This is a subtle point about weak tables. Once a userdata is "garbage",
it must be collected. That means that it must be removed from weaktables,
and its __gc must be called. The point is: what comes first? There seems
to be no "right" answer to this.

In the current implementation, we took a pragmatic solution. Values are
removed before the __gc, while keys are removed after. The rationale is
that, if you have something like "a[i] = u" and you are killing "u", you
already have "u" in your hands, so you do not need to query "a" about
it. At the same time, if someone else tries to get a[i], it gets nil,
because "u" is already being collected.

On the other hand, if you have something like "a[u] = i", then you may
need this "i" whenever you have u. So Lua keeps this entries until "u"
is actually collected (that is, after the __gc). At the same time, this
should botter no one else, since without "u" they cannot "find" this
entry in the table (unless they do a traversal...).

-- Roberto