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

QObject::deleteLater doc improvement and misuse detection

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Reported
    • Priority: Not Evaluated
    • Resolution: Unresolved
    • Affects Version/s: 5.15.5, 6.1.2
    • Fix Version/s: None
    • Labels:
      None
    • Platform/s:
      All

      Description

      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.

        Attachments

        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.

          Activity

            People

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

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes