From d026fad3d962eed0119351cd37f34490e09153fd Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 2 May 2023 18:40:17 +0200 Subject: [PATCH] QPointer: also make conversion to pointer-to-const work The QWeakPointer conversion SMFs cannot actually be used for QObject payloads, as, for unknown reasons (some comment about vtable this author doesn't understand), conversion goes through QSharedPointer, the creation of which throws the checkQObjectShared() warning and yields a nullptr. We need to continue to use the QWeakPointer(T*, bool) constructor the QPointer(T*) ctor also uses. It's high time we dissociated QPointer from QWeakPointer... Amends 5f28d367d999842a42fa0afa0d36d44ff61ea11d. Fixes: QTBUG-112464 Change-Id: I2f93843af3daf02323d77a4259eaa3745d8de3a8 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qpointer.h | 6 ++++-- tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h index ada4445d43..66088054ef 100644 --- a/src/corelib/kernel/qpointer.h +++ b/src/corelib/kernel/qpointer.h @@ -33,9 +33,11 @@ public: // compiler-generated dtor is fine! template = true> - QPointer(QPointer &&other) noexcept : wp(std::move(other.wp)) {} + QPointer(QPointer &&other) noexcept + : wp(std::exchange(other.wp, nullptr).internalData(), true) {} template = true> - QPointer(const QPointer &other) noexcept : wp(other.wp) {} + QPointer(const QPointer &other) noexcept + : wp(other.wp.internalData(), true) {} #ifdef Q_QDOC // Stop qdoc from complaining about missing function diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index 0af3723704..332cf6ab71 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -52,7 +52,7 @@ void tst_QPointer::conversion() QFile file; QPointer pf = &file; QCOMPARE_EQ(pf, &file); - QPointer pio = pf; + QPointer pio = pf; QCOMPARE_EQ(pio, &file); QCOMPARE_EQ(pio.get(), &file); QCOMPARE_EQ(pio, pf); @@ -63,7 +63,7 @@ void tst_QPointer::conversion() QFile file; QPointer pf = &file; QCOMPARE_EQ(pf, &file); - QPointer pio = std::move(pf); + QPointer pio = std::move(pf); QCOMPARE_EQ(pf, nullptr); QCOMPARE_EQ(pio, &file); QCOMPARE_EQ(pio.get(), &file);