Crash-on-exit after call to app.processEvents()

Raymond Osborn rayosborn at mac.com
Mon May 25 23:36:57 BST 2020


Hi Armando,
Thank you so much. It looks like your version of the shutdown procedure works both on my Mac and on Linux. It is no longer necessary to explicitly delete the main window and the QApplication instance, as suggested by the stackoverflow post. I think the other key thing is to do this before calling the QApplication quit() method.

I still don’t understand why the bug is triggered by calling processEvents(), but that’s an academic question now.

Thanks again,
Ray

> On May 25, 2020, at 4:35 PM, Raymond Osborn <rayosborn at mac.com> wrote:
> 
> Hi Armando,
> Good to hear from you. I thought for a while that I had managed to fix it with the following code :
> 
>     def main(filename=None):
>         app = NXConsoleApp()
>         app.initialize(filename=filename)
>         app.start()
>         for w in QApplication.topLevelWindows():
>             del w
>         del app.window, app.app
>         sys.exit(0)
> 
> It proved to be a false dawn - the problem started to recur again. I’ll see if your version catches things that mine missed, and let you know.
> 
> I have spent a lot of time trying to cover up PyQt5 bugs in the past year. The problem is that I can’t guarantee that my users will have the latest version of PyQt, so even if it is fixed in v5.14, I still have to patch the code. I just have no idea why simply calling processEvents() triggers the bug. If I knew that, perhaps it would give some clue how to work around it.
> 
> Ray
> 
> 
>> On May 25, 2020, at 3:38 PM, V. Armando Sole <sole at esrf.fr <mailto:sole at esrf.fr>> wrote:
>> 
>> Hi Ray,
>> 
>> I do not know if it will help your case. I was getting rid of some crashes on exit by deleting all widgets that had no parent. For instance, when closing the QMainWindow by doing:
>> 
>>     def closeEvent(self, event):
>>         if __name__ == "__main__":
>>             app = qt.QApplication.instance()
>>             allWidgets = app.allWidgets()
>>             for widget in allWidgets:
>>                 try:
>>                     # we cannot afford to crash here
>>                     if id(widget) != id(self):
>>                         if widget.parent() is None:
>>                             widget.close()
>>                 except:
>>                     _logger.debug("Error closing widget")
>>         return qt.QMainWindow.closeEvent(self, event)
>> 
>> Best regards,
>> 
>> Armando
>> 
>> On 25.05.2020 19:48, Raymond Osborn wrote:
>> 
>>> I added a call to QtWidgets.QApplication.instance().processEvents() in order to force adjustResize() to work when switching tabs (that's another story). It succeeded in fixing my resize issue, but now it triggers a segfault when I exit the application. This seemed to be related to the bug described in https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes-on-exit <https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes-on-exit>. One fix, suggested by @ekhumoro on stackoverflow (https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-crash-on-exit <https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-crash-on-exit>) was to delete the main window and the app first, but that doesn't seem to fix it. I am running PyQt 5.12 because that is the last version supported by conda, so I can't test the one-exit fix in v5.13, and my users probably wouldn't have it installed anyway. It happens on Macs and linux.
>>>  
>>> What I am asking is if there are additional things to try in addition to @ekhumoro's suggestion? Basically he suggests doing something like:
>>>  
>>> def main():
>>>     app = QApplication(sys.argv)
>>>     main_window = MainWindow()
>>>     main_window.show()
>>>     app.exec_()
>>>     # ensure correct deletion order
>>>     del main_window, app
>>> Does anyone understand why the crash only occurs if I have calls to processEvents() in the code? It never happens when I remove them.
>>>  
>>> Thanks,
>>> Ray
>>>  
>>>  
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200525/28d7ca02/attachment-0001.htm>


More information about the PyQt mailing list