[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Problem with circular refrence and garbage collection
- From: Receiver of Lua mailing list <jdv@...>
- Date: Mon, 12 Jan 2009 08:27:50 +0100
malobu@cox.net schreef:
> Thanks for explanation, I tried to find more details about what the
> issue is with 5.1 that is to be improved in 5.2 but couldn't find
> anything.
>
> Anyways, I did try using the userdata fenv, and that worked perfectly,
> although i'm not sure exactly why, which bothers me.
>
Well, the problem is fairly simple, and happens with tables as well as
userdata's. When you have two tables:
a = {}; b = {}
and you have a weakly keyed table
wkt = setmetatable({}, {__mode = "k"})
which you use to store references from one to the other, like this:
wkt[a] = b
wkt[b] = a
Then this is an uncollectable cycle. The problem is that the garbage
collector does not see the references as being from 'a' to 'b', and vice
versa, but simply as references from the table wkt to a and b (remember,
the keys are weak, but the values are not). The reference 'a->b' is
automatically removed when a is removed (because of the weak key), but
that never happens, because wkt still has a reference to b; the same is
true for the reference in the other direction.
This will improve in Lua 5.2, where tables with weak keys but strong
values are treated specially (as ephemeron tables).
When you use the environment tables, the garbage collector correctly
interprets the references as being from a->b and b->a, which means that
cycles are collected correctly.
Regards,
Jan