Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-80405

CRASH: winId can return a bad handle if the widget was closed already

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P1: Critical
    • 5.14.0
    • 5.12.5
    • None
    • xcode 11.1, macOS Mojave
    • macOS

    Description

      If you reuse a widget that's closed (a QDialog), and you then access its winId, it will likely crash

      #include <QtWidgets>
      #import <AppKit/AppKit.h>
      
      
      class MyWidget : public QDialog
      {
          bool event(QEvent *event) override
          {
              if (event->type() == QEvent::Show) {
                  qDebug() << "fixTitleBar called: WinID" << winId();
                  NSView* mainView = reinterpret_cast<NSView *>(winId());
                  NSLog(@"mainView %@", mainView); //accessing the bad reference
              }
              return QDialog::event(event);
          }
      };
      
      
      
      
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
          MyWidget w;
          qDebug() << 1 << w.exec();
          qDebug() << 2 << w.exec();
          return 0;
      }

      On the second call, the winId is still the same but it crashes as soon as you use winId as an NSView (dangling pointer?).

      A workaround is to define the close event as follows

          void closeEvent(QCloseEvent *e) override
          {
              e->ignore();
              hide();
          }
       

      But I'm not willing to do that since the implications are probably dear.

       

      NOTE: With Qt 5.9 this code is working fine and the reference is the also the same on the 2 calls.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vestbo Tor Arne Vestbø
            thierryb Thierry Bastian
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes