lua-users home
lua-l archive

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


On Wednesday 03 September 2003 01:03 pm, Virgil Smith wrote:
> Um, I'm not sure why you think cycles won't be garbage collected (the whole
> point of mark and sweep garbage collection, is that "unreachable" cycles
> will be garbage collected). 

Consider this situation: a global variable (in the root set) refers to a 
userdata A. This userdata uses a reference in the registry (also part of the 
root set) to a second userdata B. Userdata A has a gc metamethod that unrefs 
all its references. So this is no problem so far; if the global variable is 
removed, A will be gc'ed, and then B will be gc'ed on the next cycle.

However, now suppose that userdata B refers, through another reference in the 
registry, back to A. It also has a gc metamethod to unref its references. 
However, since both objects are now in the root set, neither one can ever be 
unreferenced and removed from the root set. It's reference-counting cycles 
all over again.

I think the idea of userdata-upvalues is a good one, and is pleasingly 
parallel to upvalues in c-functions. And based on my (very limited) work with 
the Lua source, I shouldn't think that implementing this would be especially 
hard (though it would impose some definite constraints on the programmer in 
terms of throwing around references during a gc).

Ben