PySide2 crash when creating QApplication subclass within main()
-
I have a PySide2 (5.15.2.1) app on macOS 10.14.6 that works fine under Python 3.6 and 3.7 but crashes under Python 3.8. The minimal crashing example (seg fault 11) is:
from PySide2 import QtCore, QtWidgets from PySide2.QtCore import Qt class AppSubclass(QtWidgets.QApplication): def __init__(self, *args, **kwargs): QtWidgets.QApplication.__init__(self, *args, **kwargs) def main(): app = AppSubclass() if __name__=="__main__": main()
Strangely, removing the main function results in no crash:
from PySide2 import QtCore, QtWidgets from PySide2.QtCore import Qt class AppSubclass(QtWidgets.QApplication): def __init__(self, *args, **kwargs): QtWidgets.QApplication.__init__(self, *args, **kwargs) app = AppSubclass()
Is there something not right about how I'm subclassing QApplication?
-
This turns out to likely be a bug in PySide, which has been solved. See https://bugreports.qt.io/browse/PYSIDE-1447
-
@jamesbcd
I'm afraid I think your code looks fine. And there is no reason why the second example should succeed where the first example fails. The fact it works under some Pythons but not others is troubling.You might try a couple of things just in case they resolve:
-
You are not passing anything for the
args
to your class/QApplication
. Is that defaulting in PySide2? You are supposed to pass theargv
from the command line, but at least try:app = AppSubclass([])
. Any better? -
You don't use the
kwargs
. Can you omit that from your code?
-
-
@JonB Thanks. I tried passing sys.argv to AppSubclass, but it makes no difference. When running from the terminal inside Visual Studio Code, I get a crash dump (extract below), which suggests it's something to do with garbage collection.
Process: Python [4144] Path: /Users/USER/*/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python Identifier: org.python.python Version: 3.8.12 (3.8.12) Code Type: X86-64 (Native) Parent Process: bash [866] Responsible: Electron [561] User ID: 501 Date/Time: 2022-01-30 18:34:31.840 +1000 OS Version: Mac OS X 10.14.6 (18G9323) Report Version: 12 System Integrity Protection: enabled Crashed Thread: 0 Dispatch queue: com.apple.main-thread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000009 VM Regions Near 0x9: --> __TEXT 0000000108544000-0000000108545000 [ 4K] r-x/rwx SM=COW /Users/USER/*/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 org.python.python 0x00000001086c0138 PyObject_GC_UnTrack + 24 1 org.python.python 0x00000001085c36dc subtype_dealloc + 204 2 org.python.python 0x000000010859f83f insertdict + 1007 3 libshiboken2.abi3.5.15.dylib 0x0000000108e23c2e MakeQAppWrapper + 158 4 libpyside2.abi3.5.15.dylib 0x00000001094319a4 PySide::destroyQCoreApplication() + 148 5 libpyside2.abi3.5.15.dylib 0x00000001094318ed PySide::runCleanupFunctions() + 141 6 QtCore.abi3.so 0x0000000109161ea1 SbkQtCoreModule___moduleShutdown(_object*) + 33 7 org.python.python 0x00000001085af381 cfunction_vectorcall_NOARGS + 209 8 org.python.python 0x000000010856e08c PyVectorcall_Call + 108 9 org.python.python 0x00000001086f7e11 atexit_callfuncs + 113 10 org.python.python 0x00000001086994dc Py_FinalizeEx + 76 11 org.python.python 0x00000001086be007 Py_RunMain + 1863 12 org.python.python 0x00000001086be77b pymain_main + 411 13 org.python.python 0x00000001086be7db Py_BytesMain + 43 14 libdyld.dylib 0x00007fff60f213d5 start + 1 Thread 1: 0 libsystem_kernel.dylib 0x00007fff61057bfa __workq_kernreturn + 10 1 libsystem_pthread.dylib 0x00007fff61114636 _pthread_wqthread + 458 2 libsystem_pthread.dylib 0x00007fff611143fd start_wqthread + 13 Thread 2: 0 libsystem_kernel.dylib 0x00007fff61057bfa __workq_kernreturn + 10 1 libsystem_pthread.dylib 0x00007fff611146e6 _pthread_wqthread + 634 2 libsystem_pthread.dylib 0x00007fff611143fd start_wqthread + 13 Thread 3: 0 libsystem_kernel.dylib 0x00007fff61057bfa __workq_kernreturn + 10 1 libsystem_pthread.dylib 0x00007fff61114636 _pthread_wqthread + 458 2 libsystem_pthread.dylib 0x00007fff611143fd start_wqthread + 13 Logical CPU: 0 Error Code: 0x00000004 Trap Number: 14 VIRTUAL REGION REGION TYPE SIZE COUNT (non-coalesced) =========== ======= ======= Activity Tracing 256K 1 CoreUI image file 196K 3 Dispatch continuations 8192K 1 Kernel Alloc Once 8K 1 MALLOC 40.8M 57 MALLOC guard page 16K 3 MALLOC_LARGE (reserved) 640K 2 reserved VM address space (unallocated) STACK GUARD 16K 4 Stack 17.5M 8 VM_ALLOCATE 9000K 18 __DATA 25.1M 274 __FONT_DATA 4K 1 __LINKEDIT 235.9M 32 __TEXT 184.0M 262 __UNICODE 564K 1 mapped file 44.9M 11 shared memory 636K 10 =========== ======= ======= TOTAL 567.2M 689 TOTAL, minus reserved VM space 566.6M 689 Model: MacBookAir6,2, BootROM MBA61.0107.B00, 2 processors, Intel Core i7, 1.7 GHz, 8 GB, SMC 2.13f15 Graphics: kHW_IntelHD5000Item, Intel HD Graphics 5000, spdisplays_builtin
-
This turns out to likely be a bug in PySide, which has been solved. See https://bugreports.qt.io/browse/PYSIDE-1447