[Gc] Circular references
bruce at hoult.org
Wed Feb 27 02:00:45 PST 2008
On Wed, Feb 27, 2008 at 10:40 PM, Christian Gudrian
<gn at fluidon.dyndns.info> wrote:
> "Any finalizable object that is reachable from itself by following one
> or more pointers will not be finalized (or collected)."
> To my understanding that says, that doubly linked objects neither get
> finalized nor even collected.
That is true. Don't do that.
Why do you need finalizers? Also, why do you need the list to be doubly linked?
If you really need both then there are two solutions:
- make either the forward or the backward links into "disappearing links".
- Do not have a finalizer on the list elements. Make the list
elements point to another object, and register a finalizer on that
But this is really a very strange data structure to use. With this
topology none of the links in the list, and nothing that they point
to, can ever be garbage collected until there are no external links to
any of the nodes of the list. At this point (in the absence of
finalizers) everything will be garbage collection at once.
If you want to remove an item from a doubly linked list then you have
to very explicitly perform an operation in which you set the previous
object's next pointer to the object's next pointer, and also set the
next object's prev pointer to the object's prev pointer. Once you do
that the object will be collected at the next GC if there are no other
reference to it (and it's finalizer run), but you might well want to
just do the cleanup actions at the same time as you unlink it (if you
are sure there are no other references to it).
More information about the Gc