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

QObject::deleteLater doc improvement and misuse detection

XMLWordPrintable

    • All
    • 50f128894 (dev), 97e9961e6 (6.10), 6db9ac705 (6.9), 87d03808d (tqtc/lts-6.8)

      Qt supports moving worker QObject(s) to QThread but it may be hard to find how to properly delete them in the documentation, and misguided attempts may fail silently. 

      The QObject::deleteLater documentation could be more explicit by complementing : 

      if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes.

      With: "For instance, QThread::finished() signal can be connected to QObject::deleteLater()."

      Another mention in Per-Thread Event Loop documentation would seem useful.

       

      Finally, if some user misguidedly connect the QObject::destroyed() signal of the QThread to the wroker's QObject::deleteLater() instead, Qt could emit a warning so the user can find the actual solution. It seems like changing the following 2 comments in QCoreApplication::postEvent

      // posting during destruction? just delete the event to prevent a leak

      Could be complemented with a specific warning :

      if (event->type() == QEvent::DeferredDelete)
      qWarning("QCoreApplication::postEvent: Ignored QEvent::DeferredDelete");

      The enclosed program reproduces a silent leak (confirmed with ASan) that would benefit such warning.

        1. main.cpp
          0.9 kB
        2. test_deleteLater.pro
          0.1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            docteam Qt Documentation Team
            arnaud.clere Arnaud Clère
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: