Hi Thomas,

I have already found this problem in the past, this is not a PySide
problem this is a problem on python GC its not work well with cyclic
dependency  and object which implement __del__ function, I do not know
the exactly problem on GC but I can reproduce your problem with this
simple code.


You can have more info about python GC on:
http://docs.python.org/library/gc.html

class MyObject(object):
    def __init__(self, p):
        self._parent = p
        print "created", self

    def __del__(self):
        print "del", self


class MyObject2(object):
    def __init__(self):
        self._child = MyObject(self)
        print "created", self

    def __del__(self):
        print "del", self


o = MyObject2()
del o



Then I recommend to you use a weak ref when you will store the parent
object, this will avoid the cyclic dependency. Or someone who now more
python GC  can tell you how to solve this in other way, but I normally
use a proxy object.


You can get more info about weak ref from here:
http://docs.python.org/library/weakref.html


BR



On Wed, Oct 20, 2010 at 9:00 AM, Thomas Sturm <[email protected]> wrote:
> Dear list,
>
> I am quite puzzled about my piece of code below (and attached), where none of 
> the two destructors is called when terminating the program via closeEvent(). 
> The problem disappears when I remove the assignment "self.main = parent" from 
> MyMenuBar.__init__(). I have checked this on Mac OSX and Ubuntu.
>
> I would actually expect the following to happen with or without the 
> questionable assignment:
>
> 1. The MyMenuBar instance is known as a child of the MyMainWindow instance in 
> the Qt widget hierarchy. It is thus deleted by Qt on the closeEvent().
>
> 2. On deleting that instance its reference self.main disappears, and it 
> cannot play a role for what happens subsequently.
>
> 3. Since the instance of MyWorker lives outside the Qt widget hierarchy, I 
> would expect its destructor to be called by Python on termination of the 
> program.
>
> Any clarification is highly appreciated! I am glad to have reduce my way more 
> complicated situation to that small example, but now I am clueless.
>
> NB: I know that there is a parent() method, and that in that situation the 
> assignment is pointless etc.
>
> Thomas
>
>
>
> #!/usr/bin/env python
>
> import sys
>
> from PySide.QtGui import QApplication
> from PySide.QtGui import QMainWindow
> from PySide.QtGui import QMenuBar
>
> class MyMainWindow(QMainWindow):
>    def __init__(self,parent=None):
>        super(MyMainWindow,self).__init__(parent)
>        self.worker = MyWorker()
>        self.setMenuBar(MyMenuBar(self))
>        self.show()
>        self.raise_()
>
> class MyMenuBar(QMenuBar):
>    def __init__(self,parent=None):
>        super(MyMenuBar,self).__init__(parent)
>        self.main = parent  # This line causes the problem
>
>    def __del__(self):
>        print "in MyMenuBar.__del__()"
>
> class MyWorker(object):
>    def __del__(self):
>        print "in MyWorker.__del__()"
>
> app = QApplication(sys.argv)
> mainwindow = MyMainWindow()
> sys.exit(app.exec_())
> --
> Dr. habil. Thomas Sturm
> Departamento de Matematicas, Estadistica y Computacion
> Universidad de Cantabria, Santander, Spain
> Avda. Los Castros s/n, Room 1072, +34 693 251058
> http://personales.unican.es/sturmt/
>
>
> _______________________________________________
> PySide mailing list
> [email protected]
> http://lists.openbossa.org/listinfo/pyside
>
>



-- 
Renato Araujo Oliveira Filho
Instituto Nokia de Tecnologia - INdT
Mobile: +55 (81) 8704-2144
_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to