__del__ problem - would adopting Garbage Collection fix this?

Neel Krishnaswami neelk at brick.cswv.com
Thu Apr 20 18:51:10 EDT 2000


Just van Rossum <just at letterror.com> wrote:
> 
> I'm not sure if I understood it correctly, but I think the newly
> proposed garbage collector does *not* call __del__ if the object in
> question is part of a cycle. It seems indeed impossible to do so
> correctly. I think the site explained quite well why this is, but I
> can't seem to repeat it off the top of my head.

The natural order of calling finalizers -- the one that reference
countin automatically induces -- is the topological sort of the
references graph, and there can only be a well-defined topological
sort if there are no cycles in the reference graph. So if there are
object cycles there is no obvious order of calling the finalizers
within each cycle (which algorithm junkies will note are the
strongly-connected components of the reference graph). Note also that
reference-counting *never* calls the finalizers in a cycle.

So not calling the finalizers and giving the programmer access to the
list to break the cycle manually seems like a sensible idea. An even
more sensible idea would be to drop __del__ methods altogether, since
IMO you deserve to lose if you try to use the same deallocator for
arbitrary resources that you use for memory, but that idea is probably
not going to fly. ;)


Neel



More information about the Python-list mailing list