lua-users home
lua-l archive

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

On Feb 15, 2005, at 13:02, Adam D. Moss wrote:

Hi there!

foo = {}
wtable[foo] = setmode({1, 2, foo, 4}, "k")
foo = nil
[ lots of other stuff not involving wtable]

You haven't obviously assigned wtable (to be a table value) before indexing it with foo, and you haven't explained what setmode does.

But I'll assume that you mean to put an ordinary (strong) reference to foo in a table which is only reachable by using foo as a weak key in some other (weak) table. Or something like that.

Will 'foo' be GC'able (and more to the point will Lua actually
GC it)?  The only surviving way to 'get at' foo is indirectly
through the [foo] key of wtable, and wtable is weak-keyed, so
foo is the only thing keeping itself alive and thus it might
reasonably be argued that 'foo' is GC'able.

I agree, foo should be GC'able (the GC should be permitted to remove it). In particular all paths of references from the roots to the-table-formerly-known-as-foo have at least one weak reference in the path. Or in other words there is no path of only strong (by which I mean an ordinary, not weak, reference) references from the roots to foo.

It looks like it'd probably be tough to implement such GC
semantics though, so I'm guessing that no-one does.

It's not tough, the Memory Pool System,, does it (and I'm pretty sure most of Sun's JVM GC's do it; certainly their explanation of reachability with respect to weak references makes it appear like they ought to). All you have to arrange is that you first identify the set of objects that are reachable from the roots via paths that have only strong references.

I have no idea what Lua's GC actually does.

David Jones