Generator inside a class prevent __del__ ??
Duncan Booth
me at privacy.net
Wed Apr 21 04:31:40 EDT 2004
Rob Nikander <rnikaREMOVEnder at adelphia.net> wrote in
news:kNKdnYsIEuHbdRjdRVn-uA at adelphia.com:
> I checked out the documentation for that gc.garbage list and it says
> that the collector can't free objects in cycles if the cyles have
> objects that have __del__ methods. So it puts them in this list.
>
> I wonder what other garbage collectors do in this situation? Anyone
> know? Java?
>
Most garbage collectors will do peculiar things if you have destructors or
finalizers in the objects. The problem is that if two objects with
finalizers reference each other there is no correct order to release the
objects that will guarantee that the other object still exists, so the
system either has to choose an arbitrary order, or refuse to call the
finalizers.
The .Net garbage collector is typical. Objects may have finalizers, and
these finalizers are called as part of the garbage collection. The system
guarantees that any finalizer is called exactly 0 or more times --- usually
it is called once when the object is garbage collected, but if the object
is never collected it may not be called at all, and if the object
resurrects itself (e.g. during the finalizer it assigns itself to a global
variable) the finalizer could be called more than once.
A separate thread pool is used for finalizers, so your finalizer could be
called while a user thread is executing a method on the object, and two
objects which refer to each other could have their finalizers called in any
order, or even simultaneously on separate threads. Effectively, this makes
finalizers useless in all but the most obscure situations.
When resources need to be released you should try to do it explicitly. In
.Net this is handled by the Dispose() method, and the finalizer can then
either try calling Dispose() if it has not yet been called, or could try
logging an error although even that may be problematic from a finalizer.
More information about the Python-list
mailing list