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

QObject scheduled for deletion by deleteLater doesn't get destructed.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • 5.7.0
    • 5.6.0
    • Core: Event loop
    • None

    Description

      Reproducible with the following example. When running this example a widget is shown and an object is created when you click inside this widget. When the applications state changes (for example by switching to another app) the object is scheduled for deletion. Although the application event loop is entered again the object never get's destructed during the runtime of the main event loop.

      I started to debug this issue and it seems that the allowDeferredDelete requirement inside QCoreApplicationPrivate::sendPostedEvents is never fulfilled.

      main.cpp
      #include <QApplication>
      #include <QDebug>
      #include <QWidget>
      
      
      class Object : public QObject
      {
      public:
          ~Object(){ qDebug() << "object deleted" << (quint64)this; }
      };
      
      Object *object = 0;
      
      class Widget : public QWidget
      {
      protected:
          void mousePressEvent(QMouseEvent *) override {
              if (!object) {
                  object = new Object();
                  qDebug() << "new object" << (quint64)object;
              }
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          Widget w;
          w.show();
      
          QObject::connect(qApp, &QApplication::applicationStateChanged, [](){
              if (object) {
                  qDebug() << "delete object later" << (quint64)object;
                  object->deleteLater();
                  object = 0;
              }
          });
      
          return a.exec();
      }
      

      Attachments

        Issue Links

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

          Activity

            People

              ogoffart Olivier Goffart (Woboq GmbH)
              davschul David Schulz
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes