[PyQt] Use of main() function makes otherwise identical program crash

Phil Thompson phil at riverbankcomputing.com
Wed Apr 23 17:35:46 BST 2014

On 22/04/2014 4:03 pm, rj_pyqt_qwzqfors wrote:
> I have attached a small sample PyQt program which works.
> However, if you make the change mentioned in the comments, it crashes
> when you close it, and I don't understand why.
> The program has a QMainWindow which contains a statusbar, which
> contains a toolbar, which contains a slider.
> The only difference between what works and what doesn't, is that the
> one that crashes has a main() function, while the one that works does
> not.
>  (The reason why the slider is inside a toolbar is because in the
> application I am developing, I have some toolbar buttons along with
> it.)
>  Why does this make any difference? It seems very odd to me. Is this a
> bug?
>  -- Timothy
>  P.S. I am running PyQt GPL v5.2.1 for Python v3.3 (x32) on Windows
> 8.1.

It's to do with the (random) order in which Python garbage collects the 
items of a dict - in this case the attributes of a Python "scope". Items 
can be destroyed in the wrong order for Qt resulting in a crash on exit.

PyQt5 takes steps to try and avoid this (see sip.setdestroyonexit()) but 
that has no effect if the relevant objects are not in the outermost 
scope. In your working version they are, in the non-working version they 
are instead in the scope of the main() function. PyQt4 had a way of 
dealing with this which I didn't think was necessary in PyQt5 - I was 
wrong. Should be better in tonight's snapshot.


More information about the PyQt mailing list