Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2525

Segfaults related to QMenu on macOS

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Not Evaluated
    • 6.5.4
    • 6.5.3
    • PySide
    • None
    • macOS Sonoma 14.1.1 (I saw this back in 13.x already)
    • macOS
    • 0a0f7b864 (dev), 1c207ae1c (6.6), 5ce08581c (tqtc/lts-6.5)

    Description

      I’m getting seg faults running an app on macOS that are in some way related to QMenus and Qt setting an empty menu through the AppKit interface. Those problems only appear when the app is frozen (through PyInstaller) or compiled (with Nuitka) and when started from Finder or Launchpad. They do not occur when the App is run from a Terminal.

      Here’s a minimum example:

      import sys
      
      from PySide6.QtWidgets import QApplication
      
      from PySide6.QtCore import Qt
      from PySide6.QtWidgets import (
       QDockWidget,
       QMainWindow,
       QMenu,
       QMenuBar,
      )
      
      
      class MainWindow(QMainWindow):
       def __init__(self) -> None:
       super().__init__()
      
       self.setWindowTitle("TestApp")
      
       self.menu_bar = QMenuBar(self)
      
       self.view_menu = QMenu(self.menu_bar)
       self.setMenuBar(self.menu_bar)
       # Give this menu any other title and it will work:
       self.view_menu.setTitle("View")
       self.menu_bar.addAction(self.view_menu.menuAction())
      
       self.dock_widget = QDockWidget(self)
       self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_widget)
       self.dock_widget.setWindowTitle("Dock Widget")
       self.view_menu.addAction(self.dock_widget.toggleViewAction())
      
      
      if __name__ == "__main__":
       app = QApplication(sys.argv)
       main_window = MainWindow()
       main_window.show()
       sys.exit(app.exec())

      Attached to this bug report are a pyproject.toml and a poetry.lock with the exact package versions I used. The app can be compiled with this command:

      python3 -m nuitka --enable-plugin=pyside6 --standalone --disable-console --macos-create-app-bundle crash_app.py

      When running the app (e.g., from Finder with a double click), perform the following steps to get the segfault:

      1. Grad the QDockWidget and move it out of the app so that it is floating
      2. Click on View → Dock Widget

      This will produce the crash. There are other ways that will work (like moving the app to the background and bring it back as active app). Nevertheless, the two steps described work 100% for me.

      Here’s the relevant part of the macOS crash report (full report is attached):

      Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
      
      0   libsystem_kernel.dylib               0x18843511c __pthread_kill + 8
      1   libsystem_pthread.dylib              0x18846ccc0 pthread_kill + 288
      2   libsystem_c.dylib                    0x18837ca40 abort + 180
      3   libc++abi.dylib                      0x1884246d8 abort_message + 132
      4   libc++abi.dylib                      0x1884147c8 demangling_terminate_handler() + 348
      5   libobjc.A.dylib                      0x1880bf894 _objc_terminate() + 144
      6   libc++abi.dylib                      0x188423a9c std::__terminate(void (*)()) + 16
      7   libc++abi.dylib                      0x188426a48 __cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*) + 36
      8   libc++abi.dylib                      0x1884269f4 __cxa_throw + 140
      9   libobjc.A.dylib                      0x1880b601c objc_exception_throw + 420
      10  Foundation                           0x18970547c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 188
      11  AppKit                               0x18bd158a0 -[NSMenu itemAtIndex:] + 156
      12  AppKit                               0x18bd152b4 -[NSApplication(NSWindowsMenu) setWindowsMenu:] + 124
      13  libqcocoa.dylib                      0x1099a02c8 QCocoaMenuBar::insertWindowMenu() + 264 (qcocoamenubar.mm:352)
      14  libqcocoa.dylib                      0x10999eba0 QCocoaMenuBar::updateMenuBarImmediately() + 1476 (qcocoamenubar.mm:331)
      15  QtCore                               0x104a15480 void doActivate<false>(QObject*, int, void**) + 1384 (qobject.cpp:4004)
      16  QtGui                                0x1060cb258 QGuiApplication::focusWindowChanged(QWindow*) + 24 (moc_qguiapplication.cpp:587) [inlined]

      After talking to AppKit developers, this occurs because Qt tries to set an empty NSMenu. The relevant part is line 11 where AppKit tries to find an item in the NSMenu.

      Strange enough, this does only happen, if the menu is named View (where macOS will add the “Enter Full Screen” entry), but not if I give it some other name.

      Attachments

        1. libqcocoa.dylib
          1.03 MB
        2. crash.txt
          86 kB
        3. crash_menu.py
          0.8 kB
        4. poetry.lock
          35 kB
        5. pyproject.toml
          0.4 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vestbo Tor Arne Vestbø
            tilmankrummeck Tilman Krummeck
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes