Destructor never called ???

Greg Ewing see_reply_address at something.invalid
Thu Sep 12 21:20:38 EDT 2002


Tim Roberts wrote:

> However, Python is a garbage-collected system.  Unlike in COM, an object is
> NOT destroyed as soon as its reference count goes to 0.


Actually, in CPython, it is. If your object isn't being
destroyed, it must be because there is still a reference
to it somewhere, possibly due to a cyclic data structure.

If it's a cyclic data structure, it will eventually be
found by the cyclic garbage collector. But the __del__
method *still* won't be executed, because the garbage
collector doesn't know what order the destructors of
a cyclic structure should be run in, and it doesn't
try to guess. Instead, it puts any objects it finds
which have __del__ methods into an "uncollectable
garbage" list.

So the bottom line is, don't rely on __del__ methods,
unless you're sure the object can't possibly be part
of a cyclic structure. Even then, it's not really a
good idea -- if something really must be done, do it
explicitly when it needs to be done.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list