<div dir="ltr">This is reminding me of the issues Armando raised with Matplotlib (<a href="https://github.com/matplotlib/matplotlib/issues/14178">https://github.com/matplotlib/matplotlib/issues/14178</a> / <a href="https://github.com/matplotlib/matplotlib/pull/14179">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><br></div><div>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><div><div><br><div>Tom</div></div></div></div></div><br><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">rayosborn@mac.com</a>> wrote:<br></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;">Hi Armando,<div>Good to hear from you. I thought for a while that I had managed to fix it with the following code :</div><div><br></div><div><div> def main(filename=None):</div><div> app = NXConsoleApp()</div><div> app.initialize(filename=filename)</div><div> app.start()</div><div> for w in QApplication.topLevelWindows():</div><div> del w</div><div> del app.window, app.app</div><div> sys.exit(0)</div><div><br></div><div>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><br></div><div>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><br></div><div>Ray</div><div><br></div><div><br><blockquote type="cite"><div>On May 25, 2020, at 3:38 PM, V. Armando Sole <<a href="mailto:sole@esrf.fr" target="_blank">sole@esrf.fr</a>> wrote:</div><br><div><div style="font-size:10pt;font-family:Verdana,Geneva,sans-serif"><p>Hi Ray,</p><p>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> def closeEvent(self, event):<br> if __name__ == "__main__":<br> app = qt.QApplication.instance()<br> allWidgets = app.allWidgets()<br> for widget in allWidgets:<br> try:<br> # we cannot afford to crash here<br> if id(widget) != id(self):<br> if widget.parent() is None:<br> widget.close()<br> except:<br> _logger.debug("Error closing widget")<br> return qt.QMainWindow.closeEvent(self, event)</p><p>Best regards,</p><p>Armando</p><p>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">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">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">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> </div>
<div>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> </div>
<div>
<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)"><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"><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">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"> 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">():</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">
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">=</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"> </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)">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">(</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">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">.</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">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">)</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">
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">=</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"> </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)">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">()</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">
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">.</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">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">()</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">
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">.</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">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">()</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">
</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"># 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">
</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">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"> 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">,</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"> app</span></code></pre>
<div>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> </div>
<div>Thanks,</div>
<div>Ray</div>
<div> </div>
<div> </div>
</blockquote>
</div>
</div></blockquote></div><br></div></div></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">Thomas Caswell<br><a href="mailto:tcaswell@gmail.com" target="_blank">tcaswell@gmail.com</a></div>