[PyKDE] Segmentation fault on exit

Phil Thompson phil at river-bank.demon.co.uk
Wed Oct 11 15:14:27 BST 2000


Thomas Gfüllner wrote:
> 
> Hi,
> 
> if I close the main widget I get a "Segmentation fault".
> Do you know the reason for this?
> 
> import sys
> from qt import *
> 
> class sub(QWidget):
>     def __init__(self,parent = None,name = None,fl = 0):
>         QWidget.__init__(self,parent,name,fl)
> 
>         self.resize(100,100)
>         hbox = QHBoxLayout(self)
> 
>         self.new = QPushButton(self,'new')
>         self.new.setText(self.tr('&Sub'))
>         hbox.addWidget(self.new)
> 
> class users(QWidget):
>     def __init__(self,parent = None,name = None,fl = 0):
>         QWidget.__init__(self,parent,name,fl)
> 
>         self.resize(100,100)
>         hbox = QHBoxLayout(self)
> 
>         self.new = QPushButton(self,'new')
>         self.new.setText(self.tr('&New'))
>         hbox.addWidget(self.new)
> 
> if __name__ == '__main__':
>     a = QApplication(sys.argv)
>     w = users()
>     a.setMainWidget(w)
>     w.show()
>     sub = sub()
>     sub.show()
>     a.exec_loop()

It's a bug that's triggered when you have a top-level widget that
doesn't have a parent and isn't set as the main widget (ie. sub in your
example).

What's happening is that (because of that particular set of
circumstances) Python thinks it has responsibility for deleting the sub
widget when the sub Python object is deleted. However the QApplication
dtor deletes all existing widgets and (in this case) gets in first
(because the 'a' Python object happens to be deleted before the 'sub'
object - this order is random). Therefore, by the time the sub object
gets to be deleted it tries to delete the sub widget - but it's already
been deleted, hence the segv.

The fix has been applied to the current CVS (you need to update SIP and
PyQt). Also there are 2 workarounds for this example...

First, replace the "sub = sub()" line with...

	sub = sub(w,None,Qt.WType_TopLevel)

...which means that sub has a parent so Python knows it doesn't have the
responsibility of deleting it.

Or, second, delete sub before the QApplication dtor has a go by adding
"del sub" after the "a.exec_loop()" line.

Phil




More information about the PyQt mailing list