[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Weak tables and userdata finalization
- From: Gregory Bonik <gregory@...>
- Date: Fri, 02 Sep 2011 23:11:28 +0400
It's a great coincidence: I had exactly the same problem yesterday. My
program which uses similar cache just crashed randomly. The bug was very
hard to reproduce and I spent the whole day tracking it down.
> Now, I haven't seen this happening, but the above code snippet
> certainly suggests that it's possible. Please tell me I've missed
> something as opposed to just being extremely lucky...
So yes, you were lucky :)
The solution I ended up with is just manually removing T from the cache
in the U2's finalizer.
Let me guess: I think U2 is a dummy userdata only used because one
cannot set a finalizer for a table in Lua 5.1. AFAIK, it's possible in
Lua 5.2, so this probably would solve the problem.
Concerning the explaination of behaviour, this excerpt from the manual
may be relevant (note the last sentence):
> At the end of each garbage-collection cycle, the finalizers for
> userdata are called in reverse order of their creation, among those
> collected in that cycle. That is, the first finalizer to be called is
> the one associated with the userdata created last in the program. The
> userdata itself is freed only in the next garbage-collection cycle.