[PySide] PySide application crashes when getting closed, related to QAction menu item

Sean Fisk sean at seanfisk.com
Mon Feb 24 21:00:38 CET 2014


Hi Manuel,

I would guess that it is a C++ scoping issue. I have had similar issues in
the past and it is usually due to a C++ object that needs to stick around
being dropped out of scope and then garbage-collected by Python.

Can you try storing menubar and fooMenu as attributes of self? E.g.,

#!/usr/bin/env python
import sys
from PySide import QtGui
class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self._menubar = self.menuBar()
        self._fooMenu = self._menubar.addMenu("&Foo")
        # commenting out the following two lines avoids my app to crash
        self._barAct = QtGui.QAction("Bar", self, checkable=True,
                                     statusTip="Do bar", triggered=self._onBar)
        self._fooMenu.addAction(self._barAct)

    def _onBar(self):
        "Handle toggling of bar checkbox"
        print "_onBar"
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()

This will ensure that these objects aren't garbage-collected.


--
Sean Fisk


On Mon, Feb 24, 2014 at 2:30 PM, Manuel Koch <mko0815 at gmail.com> wrote:

> Hi Sean
>
> The program ( i.e. Python interpreter just crashes ) and Windows shows a
> popup "Python.exe has stopped working." and offers means to attach Visual
> Studio debugger. I attached a debugger, but the callstack did not pin point
> to a specific issue, it just seems to have accessed memory that is not
> valid (anymore).
>
> The strange think is that I have almost stripped down my program ( from
> several thousand lines ) to just a view widgets and it runs ok when I dont
> include this particular QAction and it crashes everytime I include it ?!
>
> kind regards
> Manuel
>
> Am 24.02.2014 um 20:06 schrieb Sean Fisk <sean at seanfisk.com>:
>
> Hi Manuel,
>
> I tried your code on [Mac OS 10.9 Mavericks, PySide 1.2.1, Python 2.7.5]
> and it worked fine. Would you mind posting the error that you get?
>
> Thanks,
>
>
> --
> Sean Fisk
>
>
> On Mon, Feb 24, 2014 at 11:39 AM, Manuel Koch <mko0815 at gmail.com> wrote:
>
>> Hi
>>
>> I have a strange problem related to menu actions in my PySide application:
>> PySide 1.2.1, Python 2.7  32bit, Windows 7
>>
>> My main window uses QAction to setup menu structure.
>> One of the menu items seems to cause a crash when the application is
>> closed.
>> Following small snippet seems  to trigger app to crash immediately after
>> hitting the close button of main window !?
>>
>> I'm I doing something wrong when connecting signals/slots related to
>> QAction ?
>>
>> The code:
>>
>> class MainWindow(QMainWindow):
>>
>>     def __init__(self,parent=None):
>>         super(MainWindow,self).__init__(parent)
>>         .....
>>         menubar = self.menuBar()
>>         fooMenu = menubar.addMenu("&Foo")
>>         # commenting out the following two lines avoids my app to crash
>>         self._barAct = QAction("Bar", self, checkable=True, statusTip="Do
>> bar", triggered=self._onBar)
>>         fooMenu.addAction(self._barAct)
>>
>>     def _onBar(self):
>>         "Handle toggling of bar checkbox"
>>         print "_onBar"
>>
>>
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/pyside
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140224/18f126bb/attachment.html>


More information about the PySide mailing list