[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Finalisation of userdata in weak-valued tables
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 31 Dec 2010 14:35:27 -0200
> I have been using a weak-valued table for this purpose. I have a data
> structure like this:
> [...]
> [key] = {proxy = <userdata with __gc metamethod>}
> })
> When I want the object, I look in the cache table, and if it's not
> there, recreate it.
>
> Under some conditions, I find the object (the table containing the
> userdata) in the cache, and store a reference to it in a local variable.
> Then later, while I've still got the reference in the local variable,
> the __gc metamethod of the userdata gets called.
>
> I confirmed that the reference was still present in the local variable,
> by searching locals with debug.getlocal.
>
> This situation arises in both Lua 5.1.4 and Lua 5.2.0-alpha. Is this
> normal?
>
> I suspect that the GC decides at some point that the proxy is no longer
> reachable through strong references (a correct decision), but delays the
> call of the finaliser until later.
>
> [...]
>
> I'd like to know if this is expected behaviour, because if it is, I can
> work with it.
If I understood correctly, this behaviour is not expected. The GC really
decides at some point that the proxy is no longer reachable and delays
the call to the finalizer until later. But it clears the weak tables
atomically with the decision, so the userdata should be removed as soon
as the GC decides to finalize it, before the finalization. What are
the keys in that weak table?
-- Roberto