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

"V. Armando Sole" <[email protected]> Mon, 25 May 2020 22:38:05 +0200
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
--=_dec54b5543962cf5477d91290f674aaa
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII

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. One fix, suggested by @ekhumoro on stackoverflow (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 a
 nd 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
--=_dec54b5543962cf5477d91290f674aaa
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
=3DUTF-8" /></head><body style=3D'font-size: 10pt; font-family: Verdana,Gen=
eva,sans-serif'>
<p>Hi Ray,</p>
<p>I do not know if it will help your case. I was getting rid of some crash=
es on exit by deleting all widgets that had no parent. For instance, when c=
losing the QMainWindow by doing:</p>
<p>&nbsp;&nbsp;&nbsp; def closeEvent(self, event):<br />&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp; if __name__ =3D=3D "__main__":<br />&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app =3D qt.QApplication=
=2Einstance()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; allWidgets =3D app.allWidgets()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for widget in allWidgets:<br />&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; try:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # we cannot a=
fford to crash here<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if id(widg=
et) !=3D id(self):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp; if widget.parent() is None:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; widget.close()<b=
r />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; except:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _l=
ogger.debug("Error closing widget")<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; 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=3D"cite" style=3D"padding: 0 0.4em; border-left: #1010ff 2=
px solid; margin: 0"><!-- html ignored --><!-- head ignored --><!-- meta ig=
nored -->I added a call to&nbsp;QtWidgets.QApplication.instance().processEv=
ents() in order to force adjustResize() to work when switching tabs (that's=
 another story). It succeeded in fixing my resize issue, but now it trigger=
s a segfault when I exit the application. This seemed to be related to the =
bug described in&nbsp;<a href=3D"https://www.riverbankcomputing.com/static/=
Docs/PyQt5/gotchas.html#crashes-on-exit">https://www.riverbankcomputing.com=
/static/Docs/PyQt5/gotchas.html#crashes-on-exit</a>. One fix, suggested by =
@ekhumoro on stackoverflow (<a href=3D"https://stackoverflow.com/questions/=
59120337/why-does-pyqt-sometimes-crash-on-exit">https://stackoverflow.com/q=
uestions/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 ru=
nning PyQt 5.12 because that is the last version supported by conda, so I c=
an'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>&nbsp;</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>&nbsp;</div>
<div>
<pre class=3D"lang-py prettyprinted prettyprint" style=3D"margin-top: 0px; =
margin-bottom: 1em; padding: 12px 8px; border: 0px; font-family: Consolas, =
Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bi=
tstream 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; borde=
r-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-r=
adius: 3px; border-bottom-left-radius: 3px; color: #242729; word-wrap: norm=
al; caret-color: #242729;"><code style=3D"margin: 0px; padding: 0px; border=
: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation =
Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monos=
pace, sans-serif; font-style: inherit; font-variant-caps: inherit; font-str=
etch: inherit; line-height: inherit; vertical-align: baseline; box-sizing: =
inherit; white-space: inherit;"><span class=3D"kwd" style=3D"margin: 0px; p=
adding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-v=
ariant-caps: inherit; font-stretch: inherit; line-height: inherit; vertical=
-align: baseline; box-sizing: inherit; color: var(--blue-800);">def</span><=
span class=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-fa=
mily: inherit; font-style: inherit; font-variant-caps: inherit; font-stretc=
h: inherit; line-height: inherit; vertical-align: baseline; box-sizing: inh=
erit; color: var(--black-750);"> main</span><span class=3D"pun" style=3D"ma=
rgin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inh=
erit; font-variant-caps: inherit; font-stretch: inherit; line-height: inher=
it; vertical-align: baseline; box-sizing: inherit; color: var(--black-750);=
">():</span><span class=3D"pln" style=3D"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; bo=
x-sizing: inherit; color: var(--black-750);">
    app </span><span class=3D"pun" style=3D"margin: 0px; padding: 0px; bord=
er: 0px; font-family: inherit; font-style: inherit; font-variant-caps: inhe=
rit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;=
 box-sizing: inherit; color: var(--black-750);">=3D</span><span class=3D"pl=
n" style=3D"margin: 0px; padding: 0px; border: 0px; font-family: inherit; f=
ont-style: inherit; font-variant-caps: inherit; font-stretch: inherit; line=
-height: inherit; vertical-align: baseline; box-sizing: inherit; color: var=
(--black-750);"> </span><span class=3D"typ" style=3D"margin: 0px; padding: =
0px; border: 0px; font-family: inherit; font-style: inherit; font-variant-c=
aps: inherit; font-stretch: inherit; line-height: inherit; vertical-align: =
baseline; box-sizing: inherit; color: #2b91af;">QApplication</span><span cl=
ass=3D"pun" style=3D"margin: 0px; padding: 0px; border: 0px; font-family: i=
nherit; font-style: inherit; font-variant-caps: inherit; font-stretch: inhe=
rit; line-height: inherit; vertical-align: baseline; box-sizing: inherit; c=
olor: var(--black-750);">(</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-=
variant-caps: inherit; font-stretch: inherit; line-height: inherit; vertica=
l-align: baseline; box-sizing: inherit; color: var(--black-750);">sys</span=
><span class=3D"pun" style=3D"margin: 0px; padding: 0px; border: 0px; font-=
family: inherit; font-style: inherit; font-variant-caps: inherit; font-stre=
tch: inherit; line-height: inherit; vertical-align: baseline; box-sizing: i=
nherit; color: var(--black-750);">.</span><span class=3D"pln" style=3D"marg=
in: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inher=
it; font-variant-caps: inherit; font-stretch: inherit; line-height: inherit=
; vertical-align: baseline; box-sizing: inherit; color: var(--black-750);">=
argv</span><span class=3D"pun" style=3D"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: var(--black-750);">)</span><span class=3D"pln" sty=
le=3D"margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-st=
yle: inherit; font-variant-caps: inherit; font-stretch: inherit; line-heigh=
t: inherit; vertical-align: baseline; box-sizing: inherit; color: var(--bla=
ck-750);">
    main_window </span><span class=3D"pun" style=3D"margin: 0px; padding: 0=
px; border: 0px; font-family: inherit; font-style: inherit; font-variant-ca=
ps: inherit; font-stretch: inherit; line-height: inherit; vertical-align: b=
aseline; box-sizing: inherit; color: var(--black-750);">=3D</span><span cla=
ss=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-family: in=
herit; font-style: inherit; font-variant-caps: inherit; font-stretch: inher=
it; line-height: inherit; vertical-align: baseline; box-sizing: inherit; co=
lor: var(--black-750);"> </span><span class=3D"typ" style=3D"margin: 0px; p=
adding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-v=
ariant-caps: inherit; font-stretch: inherit; line-height: inherit; vertical=
-align: baseline; box-sizing: inherit; color: #2b91af;">MainWindow</span><s=
pan class=3D"pun" style=3D"margin: 0px; padding: 0px; border: 0px; font-fam=
ily: inherit; font-style: inherit; font-variant-caps: inherit; font-stretch=
: inherit; line-height: inherit; vertical-align: baseline; box-sizing: inhe=
rit; color: var(--black-750);">()</span><span class=3D"pln" style=3D"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: var(--black-750);">
    main_window</span><span class=3D"pun" style=3D"margin: 0px; padding: 0p=
x; border: 0px; font-family: inherit; font-style: inherit; font-variant-cap=
s: inherit; font-stretch: inherit; line-height: inherit; vertical-align: ba=
seline; box-sizing: inherit; color: var(--black-750);">.</span><span class=
=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-family: inhe=
rit; font-style: inherit; font-variant-caps: inherit; font-stretch: inherit=
; line-height: inherit; vertical-align: baseline; box-sizing: inherit; colo=
r: var(--black-750);">show</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-=
variant-caps: inherit; font-stretch: inherit; line-height: inherit; vertica=
l-align: baseline; box-sizing: inherit; color: var(--black-750);">()</span>=
<span class=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-f=
amily: inherit; font-style: inherit; font-variant-caps: inherit; font-stret=
ch: inherit; line-height: inherit; vertical-align: baseline; box-sizing: in=
herit; color: var(--black-750);">
    app</span><span class=3D"pun" style=3D"margin: 0px; padding: 0px; borde=
r: 0px; font-family: inherit; font-style: inherit; font-variant-caps: inher=
it; font-stretch: inherit; line-height: inherit; vertical-align: baseline; =
box-sizing: inherit; color: var(--black-750);">.</span><span class=3D"pln" =
style=3D"margin: 0px; padding: 0px; border: 0px; font-family: inherit; font=
-style: inherit; font-variant-caps: inherit; font-stretch: inherit; line-he=
ight: inherit; vertical-align: baseline; box-sizing: inherit; color: var(--=
black-750);">exec_</span><span class=3D"pun" style=3D"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: var(--black-750);">()</span><span cl=
ass=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-family: i=
nherit; font-style: inherit; font-variant-caps: inherit; font-stretch: inhe=
rit; line-height: inherit; vertical-align: baseline; box-sizing: inherit; c=
olor: var(--black-750);">
    </span><span class=3D"com" style=3D"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: var(--black-400);"># ensure correct deletion order=
</span><span class=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px;=
 font-family: inherit; font-style: inherit; font-variant-caps: inherit; fon=
t-stretch: inherit; line-height: inherit; vertical-align: baseline; box-siz=
ing: inherit; color: var(--black-750);">
    </span><span class=3D"kwd" style=3D"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: var(--blue-800);">del</span><span class=3D"pln" st=
yle=3D"margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-s=
tyle: inherit; font-variant-caps: inherit; font-stretch: inherit; line-heig=
ht: inherit; vertical-align: baseline; box-sizing: inherit; color: var(--bl=
ack-750);"> main_window</span><span class=3D"pun" style=3D"margin: 0px; pad=
ding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-var=
iant-caps: inherit; font-stretch: inherit; line-height: inherit; vertical-a=
lign: baseline; box-sizing: inherit; color: var(--black-750);">,</span><spa=
n class=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; font-famil=
y: inherit; font-style: inherit; font-variant-caps: inherit; font-stretch: =
inherit; line-height: inherit; vertical-align: baseline; box-sizing: inheri=
t; color: var(--black-750);"> app</span></code></pre>
<div>Does anyone understand why the crash only occurs if I have calls to pr=
ocessEvents() in the code? It never happens when I remove them.</div>
</div>
<div>&nbsp;</div>
<div>Thanks,</div>
<div>Ray</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
</blockquote>
</body></html>

--=_dec54b5543962cf5477d91290f674aaa--