__del__ not working with cyclic reference? (and memory-leaked?)

Jane Austine janeaustine50 at hotmail.com
Wed Jul 2 01:46:18 EDT 2003


"Aahz" <aahz at pythoncraft.com> wrote in message news:bdtlji$pmh$1 at panix2.panix.com...
> In article <ba1e306f.0307011837.269e3d2a at posting.google.com>,
> Jane Austine <janeaustine50 at hotmail.com> wrote:
> >
> >I have some code using singleton pattern. The singleton instance
> >is shared as a class variable. The problem is that the singleton
> >instance is not cleared automatically.
> >
> >Following is a simplified version that shows the problem:
> >
> >--------
> >#foobar.py
> >class FooBar:
> >    def __del__(self):
> >        print "FooBar removed"
> >
> >FooBar.foobar=FooBar()
> >--------
> >
> >Why is this so? Due to the cyclic reference? Isn't python's gc
> >supposed to treat it? What singleton idiom is recommended 
> >otherwise?
> 
> There are two issues that you'll run into with this: gc doesn't work on
> classes that define __del__(), and prior to Python 2.3, gc doesn't run
> on exit.
> -- 
> Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/
> 

I read the gc module reference thoroughly and
thanks to your short comment, I could understand it.
(I should not use __del__ for singleton patterns, but
any other alternatives?)

But does the fact that __del__ is not called for some
objects mean they are not freed from memory and therefore
memory-leaked?




More information about the Python-list mailing list