[PyKDE] ref counting

Phil Thompson phil at riverbankcomputing.co.uk
Sat Feb 14 07:28:00 GMT 2004


On Saturday 14 February 2004 04:46, Stefan Seefeld wrote:
> hi there,
>
> I'm experimenting with pyqt and I'm running in
> some strange behavior.
> The examples3/canvas/canvas.py demo contains
> the following lines:
>
>      def newView(self):
>          self.m=Main(self.canvas,None,"new windiw",Qt.WDestructiveClose)
>          qApp.setMainWidget(self.m)
>          self.m.show()
>          qApp.setMainWidget(None)
>
> What I'm wondering about is the reason for the 'Main' instance to
> be stored in an attribute. In fact, if I replace 'self.m' by 'm'
> the applet doesn't work any more, i.e. as soon as I create the
> second view the first will be destructed. What's the reason for
> this ? It appears the ref count for the view drops to zero so
> it gets destroyed...

Correct, 'm' is garbage collected when newView() returns, 'self.m' is garbage 
collected when 'self' is garbage collected.

> But in the above, it would seem the second call to 'newView' would
> free the first view, and thus destroy it, too. That, however, is
> not the case.

But the second call to newView() has a different self.

> The reason I'm asking is because I'm using a modified version
> of the canvas.py demo, where this behavior is even more disturbing,
> i.e. I don't get more than one view at any given time.

Phil




More information about the PyQt mailing list