Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-20885 Larger changes for Qt 5
  3. QTBUG-22622

Cannot put a QObject* into a shared pointer after putting it into a qobject-tracking qweakpointer or a QPointer

    XMLWordPrintable

Details

    Description

      This segfaults:

      #include <QtCore>
      
      int main(int argc, char **argv)
      {
        QObject *o = new QObject;
        QWeakPointer<QObject> wp( o );
        QSharedPointer<QObject> sp( o );
      }
      

      QPointer<T> does not segfault.

      Because QPointer in Qt 5 is implemented in terms of QWeakPointer, this problem manifests itself in several more places. Some of the consequences of this can be seen using git grep QPointer. A non-exhaustive list:

      • Putting the result of myPluginLoader::instance() into a QSharedPointer aborts (this is true since Qt 4.7.0)
      • Can't put a model into a QSharedPointer after putting it into a QItemSelectionModel
      • Can't put any QObject into a QSharedPointer after using it as an event filter with installEventFilter.

      From there the effect snowballs.

      Because of holding a QPointer:

      • Can't put a QNetworkAccessManager into a QSharedPointer after creating a QNetworkReply.
      • Can't create an asyncronous DBus call with callback and then put the receiver into a QSharedPointer.

      Because of the event filter issue:

      • Can't put a running QStateMachine which contains a QEventTransition into a QSharedPointer.
      • Can't put a QFontDialog into a QSharedPointer (also not much reason to, but...)

      These are just the obvious effects of this change in Qt 5.

      There will certainly be more subtle side-effects and breakages resulting from implementing QPointer using QWeakPointer.

      The attached tests pass in Qt 4.8 and all fail with Qt 5.

      Attachments

        For Gerrit Dashboard: QTBUG-22622
        # Subject Branch Project Status CR V

        Activity

          People

            earthdomain Earth Domain (Inactive)
            stephen.kelly Stephen Kelly (Unused account) (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes