[PyKDE] Python references and the GC

Kaleb Pederson kibab at icehouse.net
Wed Oct 30 17:12:01 GMT 2002


My object _definitely_ gets garbage collected if I don't add that in
(using Python 2.2.1).  As soon as my function completes that creates my
modeless/parentless dialog my newly created dialog just flashes on the
screen before it is destroyed.  Adding self._keep = self keeps the form
in existence.

Although I can't the messages/webpage that originally gave me this idea,
but I didn't see another good way to do this other than keeping a list
of "alive" objects in the parent and then deleting the given object upon
receipt of a closing signal.

--Kaleb

-----Original Message-----
From: Mikael Schönenberg [mailto:micke at ratthing-b3cd.strakt.com] On
Behalf Of Mikael Schönenberg
Sent: Wednesday, October 30, 2002 12:54 AM
To: Kaleb Pederson
Cc: pykde at mats.gmd.de
Subject: Re: [PyKDE] Python references and the GC

On Tue, 29 Oct 2002, Kaleb Pederson wrote:

> I have a class similar to the below that keeps a reference to itself
so that
> it doesn't get garbage collected.

If my understanding of Python's garbage collection is what I hoped it
was,
your assumption here is rather optimistic.

In essence, if I understand things correctly, the garbage collector is
"smart" in the way that it will identify cyclic references of otherwise
inaccessible objects. Beazley's "Python Essential Reference" illustrates
this with the following example:

a = {}
b = {}
a['b'] = b
b['a'] = a
del a
del b

Now, since the dictionaries refer to eachother, the reference count of
both is indeed greated than 0, but since the objects are not accessible
from any other namespace, the garbage collector will trash them anyhow.

Not digging further into the rest of your code, I believe that this is
what bit you. Keep a reference to the dialog elsewhere, and don't rely
on
it staying alive by just having the line:

  self._keep = self

/micke




More information about the PyQt mailing list