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

processEvents() processes DeferredDeletes

    XMLWordPrintable

Details

    Description

      Documentation here:

      https://doc.qt.io/qt-6/qcoreapplication.html#processEvents

      says:

      "In the event that you are running a local loop which calls this function continuously, without an event loop, the DeferredDelete events will not be processed."

      However, it does process them if deleteLater() was called during another call to processEvents().

      For example, it will destroy O during the second call to processEvents:

      class O : public QObject {
          ~O(){ qDebug("O has been destroyed"); }
      };
      int main(int argc, char **argv) {
          QApplication app(argc, argv);
          QTimer::singleShot(0, [] {
              auto o = new O;
              QTimer::singleShot(0, [o]{
                  o->deleteLater();
                  qDebug("delete later called");
              });
              qApp->processEvents();
              qDebug("first processEvents() done");
              qApp->processEvents();
              qDebug("second processEvents() done");
          });
          app.exec();
      }
      

      It does say "without an event loop", but that is not true either as removing the app.exec() still has the same behavior, although it needs one extra processEvents() call. For example:

          auto o = new O;
          QTimer::singleShot(0, [o]{
              o->deleteLater();
              qDebug("delete later called");
          });
          qApp->processEvents();
          qDebug("first processEvents() done");
          qApp->processEvents();
          qDebug("second processEvents() done");
          qApp->processEvents();
          qDebug("third processEvents() done");
      

      I don't know if this is just out dated documentation or a bug, but this behavior can cause a crash. If this is just a documentation issue, maybe there should also be some way to tell processEvents to ignore deferred deletes.

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            poikelin Joni Poikelin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes