[PyQt] Bug report: multiple QApplication instances cause a segfault

David Townshend aquavitae69 at gmail.com
Wed Jun 22 20:49:39 BST 2011

On Wed, Jun 22, 2011 at 2:34 PM, Baz Walter <bazwal at ftml.net> wrote:

> On 21/06/11 17:04, David Townshend wrote:
>> The problem is that creating (and quitting) multiple QApplications in
>> succession causes a segfault.  This situation tends to occur in unit
>> tests,
>> particularly in testing subclasses of QApplication.
>> The following snippet illustrates the problem:
>>  >>> from PyQt4 import QtGui
>>>>> >>> app = QtGui.QApplication([])
>>>>> >>> app.quit()
>>>>> >>> app = QtGui.QApplication([])
>>>>> >>> app.quit()
>>>>> >>> app = QtGui.QApplication([])
>>>> Segmentation fault
> according to the qt docs, only one application object should be created at
> a time, and there is a global qApp pointer which refers to the current
> instance.
> this suggests that, in pyqt, something like the following should work:
> from PyQt4 import QtGui
> QtGui.qApp = app = QtGui.QApplication([])
> app.quit()
> QtGui.qApp = app = None
> ______________________________**_________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.**com/mailman/listinfo/pyqt<http://www.riverbankcomputing.com/mailman/listinfo/pyqt>

I've investigated further and found that its caused by the gtk style which I
am using, since I am running gnome. Simply changing the qt style avoids the
problem. I recall reading about similar issues before with the gtk-qt style,
so I think that this is a know problem.  However, I noticed that after
changing the style, python still segfaults on exit.  gdb indicates that it
is something in the QApplication destructor, and it seems that deleting the
instance after quitting solves this, e.g.:

>>> from PyQt4.QtGui import QApplication
>>> for i in range(10):
...     app = QApplication([])
...     app.quit()
...     del app
>>> exit()

I experimented a bit with setting qApp = None, but it didn't make a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110622/f6ddacd7/attachment.html>

More information about the PyQt mailing list