GC and finalizers [was: No destructor]

Paul Duffin pduffin at hursley.ibm.com
Thu Aug 24 06:57:02 EDT 2000


Neil Schemenauer wrote:
> 
> Martin von Loewis <loewis at informatik.hu-berlin.de> wrote:
> >Neil Schemenauer's collector does not collect a cycle if it involves
> >finalization of an instance object.
> 
> Real life is a little more complicated than that.  The GC tries
> quite hard to do the right thing.  For example, consider the
> garbage structure crudely drawn below:
> 
>       .->A-->C
>       |  |
>       B<-'
> 
> If only C has a finalizer then the structure will be collected
> and C's finalizer called.  If either A or B have finalizers then
> the structure is not collected but instead linked to the
> gc.garbage list.  Theoretically it could also be collected if
> either A or B had finalizers but not both.  Unfortunately
> Python's collector is not that smart.
> 
> The basic algorithm goes as follows:  Once garabage is found, all
> objects with finalizers and objects reachable from these objects
> are moved to a special "finalizer reachable" set.  The remaining
> garbage objects are freed.  This may cause the refcounts of some
> objects in the finalizer reachable to go to zero and they will be
> freed. 

What happens if after calling the finalizer on an object which has
a zero reference count the object has a non-zero reference count ?

> The remaining instance objects in the finalizer reachable
> set will be added to gc.garbage.
> 
> For all the gory details, search the python-dev archives for the
> term "finalizers".
> 

Even if A and B both had finalisers then it could be cleaned up, you
just have to be careful and set out various dos and don'ts for 
finalisers. 

Can you give an example of finalisers for A and B which would cause 
problems ?



More information about the Python-list mailing list