Destructor never called ???

Tim Peters tim.one at comcast.net
Fri Sep 20 15:19:33 EDT 2002


[Tim]
> It's only the cases where Python can't possibly guess a safe order in
> which to call destructors where both __del__ and collection are
> inhibited.

[Alex Martelli]
> I think it's a bit more than that: Python *COULD" "possibly guess a
> safe order" when there is only exactly ONE object with a __del__ that
> is part of the cycle -- namely, destroy that one object first.  It
> doesn't seem to even TRY (note: I'm not saying it SHOULD -- but
> "can't possibly" might be a bit too strong as a description of what
> is actually going on).

I agree.  Way back when, the idea was to compute the maximal strongly
connected components (SCCs) of the trash reachability graph.  The derived
graph (viewing each SCC as a node in its own right) is necessarily a DAG (no
cycles), and cleanup would then proceed in an order consistent with some
topological sort of the DAG nodes (no cycles implies a topsort is always
possible).  It was noted at the time that if the set of objects
corresponding to a single SCC "supernode" contained at most one object with
a __del__, then a safe teardown order for those objects could be known (just
as you suggest).

In practice, computing SCCs is a PITA, and the simpler scheme actually
implemented does all of that *except* for dealing with would have been SCC
DAG nodes corresponding to a set of objects exactly one of which has a
__del__, had SCC DAG nodes been constructed.  Refcount semantics
automatically tear down everything else in what would have been SCC DAG node
order (btw, for those who haven't thought about it, note that "a refcount"
is exactly the same as "a predecessor count" in a topological sort
algorithm:  they have a lot in common).

Since you and I are approximately the only two people on the newsgroup whose
heads haven't exploded from trying to understand exactly what the hell I'm
talking about there <wink>, I think it's a Positive Good that Neil skipped
all the SCC complications.  "Cycles and destructors:  don't even think about
mixing them" is much easier to remember, and has never done me any harm in
real life.





More information about the Python-list mailing list