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

QPointer cannot be used with virtual inheritance

    XMLWordPrintable

Details

    • 5

    Description

      The following code fails to compile:

          struct Derived : virtual QObject {};
      
          QPointer<Derived> pd = new Derived;
          delete pd.get();                         // L76
          QPointer<const QObject> po = pd;
          QVERIFY(!po);
      

      with

      qpointer.h: In instantiation of ‘T* Qt6::QPointer<T>::data() const [with T = tst_QPointer::conversion()::Derived]’:
      qpointer.h:55:14: required from ‘T* Qt6::QPointer<T>::get() const [with T = tst_QPointer::conversion()::Derived]
      tst_qpointer.cpp:76:22: required from here
      qpointer.h:53:14: error: cannot convert from pointer to base class ‘Qt6::QObject’ to pointer to derived class ‘tst_QPointer::conversion()::Derived’ because the base is virtual
      53 |

      Unknown macro: { return static_cast<T*>(wp.internalData()); }
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      The reason is the SCARY optimization whereby a QPointer<T> is implemented in terms of QWeakPointer<const QObject>, which, in get(), has a static_cast<T*> from the QObject* returned by QWeakPointer<const QObject>::get(). If QObject is a virtual base, this cast is invalid.

      The fix would be to undo the SCARY optimization, for which QTBUG-113366 might come in handy.

      Acceptance criteria:

      • The above code snippet compiles and is asan/valgrind-clean.

      Attachments

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

        Activity

          People

            cnn Qt Core & Network
            mmutz Marc Mutz
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes