diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 1323dd6b1c3..bd98cb326ca 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -499,7 +499,7 @@ private: template inline void enableSharedFromThis(const QEnableSharedFromThis *ptr) { - ptr->initializeFromSharedPointer(*this); + ptr->initializeFromSharedPointer(constCast::type>()); } inline void enableSharedFromThis(...) {} diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 77418032245..b63485e91eb 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -94,18 +94,18 @@ private slots: void creatingQObject(); void mixTrackingPointerCode(); void reentrancyWhileDestructing(); - - void threadStressTest_data(); - void threadStressTest(); void map(); void hash(); - void validConstructs(); - void invalidConstructs_data(); - void invalidConstructs(); - void qvariantCast(); void sharedFromThis(); + void threadStressTest_data(); + void threadStressTest(); + void validConstructs(); + void invalidConstructs_data(); + void invalidConstructs(); + // let invalidConstructs be the last test, because it's the slowest; + // add new tests above this block public slots: void cleanup() { safetyCheck(); } @@ -231,6 +231,14 @@ void tst_QSharedPointer::basics() QCOMPARE(sizeof(weakref), 2*sizeof(void*)); } + { + QSharedPointer ptr; + QWeakPointer weakref; + + QCOMPARE(sizeof(ptr), 2*sizeof(void*)); + QCOMPARE(sizeof(weakref), 2*sizeof(void*)); + } + QFETCH(bool, isNull); Data *aData = 0; if (!isNull) @@ -2171,6 +2179,16 @@ void tst_QSharedPointer::sharedFromThis() QVERIFY(const_scp.isNull()); QCOMPARE(Data::generationCounter, generations + 1); QCOMPARE(Data::destructorCounter, destructions); + + QWeakPointer wcp = sc.sharedFromThis(); + QVERIFY(wcp.isNull()); + QCOMPARE(Data::generationCounter, generations + 1); + QCOMPARE(Data::destructorCounter, destructions); + + QWeakPointer const_wcp = sc.sharedFromThis(); + QVERIFY(const_wcp.isNull()); + QCOMPARE(Data::generationCounter, generations + 1); + QCOMPARE(Data::destructorCounter, destructions); } QCOMPARE(Data::generationCounter, generations + 1); @@ -2182,6 +2200,11 @@ void tst_QSharedPointer::sharedFromThis() QVERIFY(const_scp.isNull()); QCOMPARE(Data::generationCounter, generations + 2); QCOMPARE(Data::destructorCounter, destructions + 1); + + QWeakPointer const_wcp = sc.sharedFromThis(); + QVERIFY(const_wcp.isNull()); + QCOMPARE(Data::generationCounter, generations + 2); + QCOMPARE(Data::destructorCounter, destructions + 1); } QCOMPARE(Data::generationCounter, generations + 2); @@ -2373,6 +2396,21 @@ void tst_QSharedPointer::sharedFromThis() QCOMPARE(Data::generationCounter, generations + 5); QCOMPARE(Data::destructorCounter, destructions + 5); + + { + QSharedPointer scp2(new SomeClass()); + QVERIFY(!scp2.isNull()); + QCOMPARE(Data::generationCounter, generations + 6); + QCOMPARE(Data::destructorCounter, destructions + 5); + + QWeakPointer wcp2(scp2.constCast()); + QVERIFY(!wcp2.isNull()); + QCOMPARE(Data::generationCounter, generations + 6); + QCOMPARE(Data::destructorCounter, destructions + 5); + } + + QCOMPARE(Data::generationCounter, generations + 6); + QCOMPARE(Data::destructorCounter, destructions + 6); } namespace ReentrancyWhileDestructing {