Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.15.13, 6.5.0
-
None
-
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.