How to check what is holding reference to object

Michal M mich.mierzwa at googlemail.com
Tue Apr 27 17:42:44 EDT 2010


On 27 Kwi, 23:21, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Michal M <mich.mier... at googlemail.com> wrote:
> > Hi
>
> > I've just found out that one of objects is not destroyed when it
> > should be. This means that something was holding reference to this
> > object or part of it (i.e. method). Is there any way to check what
> > holds that reference? I am unable to do that just looking to the code
> > or debugging it because it is pretty complicated, but I am able to
> > invoke this situation again.
>
> See if this code helps:
>
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> It's pretty old so it may need some adjustment, but I wrote it
> to figure out exactly that sort of problem.

Thanks you for answers.
I tried to use gc.get_referrers(self) in point in code when according
to me object should be destroyed earlier but I did not see anything
interesting. Then just realised that gc will not destroy object even
when itself holds reference to his own method.

For example

class A(object):
  def a(self):
    pass
  def b(self):
    self.method = self.a
  def __del__(self):
    print "A object deleted"

>> a = A()
>> del a
A object delted
>> a = A()
>> a.b()
>> del a
... nothing ...

I thought gc would discover such circle loops but apparently it did
not.



More information about the Python-list mailing list