<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Armando,</div><div class="">I’m writing again because I’ve learned a couple of useful things that might be worth sharing. I had a scare when the problem started occurring again for no apparent reason, using the same sequence I had tested before. I decided to look more closely in the debugger and discovered that the exit process never fully completed if one of the windows that had been resized following a  processEvents() call was still open. Somewhere between the ‘event.accept()’ call in the main window’s closeEvent function and completing the app._exec() call, it just quit, sometimes with a segfault, sometimes without. </div><div class=""><br class=""></div><div class="">The offending windows had all been opened with the main window as parent, but when I made them top-level windows, the exit process completed successfully. I had tried that already before contacting the mailing list. I think the combination of opening the offending windows without a parent and then closing all top-level widgets, as you suggested, may have finally fixed the problem. </div><div class=""><br class=""></div><div class="">Not sure if that’s the final word though.</div><div class=""><br class=""></div><div class="">Thanks again,</div><div class="">Ray</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 25, 2020, at 5:42 PM, Thomas Caswell <<a href="mailto:tcaswell@gmail.com" class="">tcaswell@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This is reminding me of the issues Armando raised with Matplotlib (<a href="https://github.com/matplotlib/matplotlib/issues/14178" class="">https://github.com/matplotlib/matplotlib/issues/14178</a> / <a href="https://github.com/matplotlib/matplotlib/pull/14179" class="">https://github.com/matplotlib/matplotlib/pull/14179</a>).  I now understand why Matplotlib (and IPython) holding references to the qApp can be problematic (as in this case del ing app just drops your reference to it but does not actually trigger the `__delete__` code because other parts of the codebase are keeping it alive) if having a "live" app on process shut down can cause problems.<div class=""><br class=""></div><div class="">My naive guess as to why calling `ProcessEvents` triggers the issue as that it creates some extra state on at least the c++ side and we end up tearing down the objects on the Python side in a different order than we need to on the c++ side.  Oventually something winds up with a null pointer (because there was a reference at the c++ level but not at the Python level) and boom.  I stress that while that is where I would start looking to track this down it is still very much a guess.<br class=""><div class=""><div class=""><br class=""><div class="">Tom</div></div></div></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 25, 2020 at 5:36 PM Raymond Osborn <<a href="mailto:rayosborn@mac.com" class="">rayosborn@mac.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class="">Hi Armando,<div class="">Good to hear from you. I thought for a while that I had managed to fix it with the following code :</div><div class=""><br class=""></div><div class=""><div class="">    def main(filename=None):</div><div class="">        app = NXConsoleApp()</div><div class="">        app.initialize(filename=filename)</div><div class="">        app.start()</div><div class="">        for w in QApplication.topLevelWindows():</div><div class="">            del w</div><div class="">        del app.window, app.app</div><div class="">        sys.exit(0)</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Ray</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On May 25, 2020, at 3:38 PM, V. Armando Sole <<a href="mailto:sole@esrf.fr" target="_blank" class="">sole@esrf.fr</a>> wrote:</div><br class=""><div class=""><div style="font-size:10pt;font-family:Verdana,Geneva,sans-serif" class=""><p class="">Hi Ray,</p><p class="">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:</p><p class="">    def closeEvent(self, event):<br class="">        if __name__ == "__main__":<br class="">            app = qt.QApplication.instance()<br class="">            allWidgets = app.allWidgets()<br class="">            for widget in allWidgets:<br class="">                try:<br class="">                    # we cannot afford to crash here<br class="">                    if id(widget) != id(self):<br class="">                        if widget.parent() is None:<br class="">                            widget.close()<br class="">                except:<br class="">                    _logger.debug("Error closing widget")<br class="">        return qt.QMainWindow.closeEvent(self, event)</p><p class="">Best regards,</p><p class="">Armando</p><p class="">On 25.05.2020 19:48, Raymond Osborn wrote:</p>
<blockquote type="cite" style="padding:0px 0.4em;border-left:2px solid rgb(16,16,255);margin:0px" class="">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 <a href="https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes-on-exit" target="_blank" class="">https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes-on-exit</a>. One fix, suggested by @ekhumoro on stackoverflow (<a href="https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-crash-on-exit" target="_blank" class="">https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-crash-on-exit</a>) 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.
<div class=""> </div>
<div class="">What I am asking is if there are additional things to try in addition to @ekhumoro's suggestion? Basically he suggests doing something like:</div>
<div class=""> </div>
<div class="">
<pre style="margin-top:0px;margin-bottom:1em;padding:12px 8px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:3px;color:rgb(36,39,41)" class=""><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:inherit" class=""><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">def</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""> main</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">():</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    app </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">=</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""> </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(43,145,175)" class="">QApplication</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">(</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">sys</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">.</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">argv</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">)</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    main_window </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">=</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""> </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(43,145,175)" class="">MainWindow</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">()</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    main_window</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">.</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">show</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">()</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    app</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">.</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">exec_</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">()</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""># ensure correct deletion order</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">
    </span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">del</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""> main_window</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class="">,</span><span style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit" class=""> app</span></code></pre>
<div class="">Does anyone understand why the crash only occurs if I have calls to processEvents() in the code? It never happens when I remove them.</div>
</div>
<div class=""> </div>
<div class="">Thanks,</div>
<div class="">Ray</div>
<div class=""> </div>
<div class=""> </div>
</blockquote>
</div>
</div></blockquote></div><br class=""></div></div></blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class="gmail_signature">Thomas Caswell<br class=""><a href="mailto:tcaswell@gmail.com" target="_blank" class="">tcaswell@gmail.com</a></div>
</div></blockquote></div><br class=""></div></body></html>