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

Crash when QWidget::winId is requested when destroying

    XMLWordPrintable

Details

    • 9ecc595d7185ad3d072f3d811d7aa52fc6992cca (qt/qtbase/5.14)

    Description

      When parent widget does not already contain any internal win id, but one of its children widgets is being destroying, => thus clears win ID => this sends QEvent::WinIdChange, and if QWIdget::winId() is requested on this event => produces a crash.

      class WinIdChangeSpy : public QObject
      {
          Q_OBJECT
          QWidget *m_widget = nullptr;
          WId m_winId = 0;
      public:
          explicit WinIdChangeSpy(QWidget *widget, QObject *parent = nullptr)
              : QObject(parent)
              , m_widget(widget)
              , m_winId(widget->winId())
          {
          }
      
          WId winId() const { return m_winId; }
      
      public slots:
          bool eventFilter(QObject *obj, QEvent *event) override
          {
              if (obj == m_widget) {
                  if (event->type() == QEvent::WinIdChange) {
                      m_winId = m_widget->winId();
                      return true;
                  }
              }
              return false;
          }
      };
      
      class WinIdCloseWidget : public QWidget
      {
          Q_OBJECT
      public:    
          QObject *m_notifier = nullptr;
          QWidget *m_deleteWidget = nullptr;
      
          WinIdCloseWidget(QWidget *parent = nullptr) : QWidget(parent), m_notifier(new QObject(this)) 
          {
              m_deleteWidget = new QWidget(new QWidget(this));
              connect(m_notifier, &QObject::destroyed, [this] { delete m_deleteWidget; });
              WinIdChangeSpy *spy = new WinIdChangeSpy(m_deleteWidget, this);
              m_deleteWidget->installEventFilter(spy);
          }
      };
      
      int main()
      {
          auto widget = new WinIdCloseWidget;
          widget->show();
          widget->windowHandle()->close();
          delete widget;
      }

      https://codereview.qt-project.org/c/qt/qtbase/+/280815

      Attachments

        1. windId_close.tar.gz
          1 kB
          Valentyn Doroshchuk
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            valentyn.doroshchuk Valentyn Doroshchuk
            valentyn.doroshchuk Valentyn Doroshchuk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes