From 1b3a74e74237d004527c3f296b7ec65365be0f25 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 16 Aug 2012 17:06:16 +0100 Subject: QSharedPointer: make QT_SHAREDPOINTER_TRACK_POINTERS work with QObjects If setQObjectShared crashes because a QObject is tracked by two different QSharedPointers, we lose the debug feature offered by #defining QT_SHAREDPOINTER_TRACK_POINTERS, as the check done by this define happens after the setQObjectShared call. Therefore, move setQObjectShared after the internalSafetyCheckAdd2 call. This way, the error raised by QSharedPointer created changes from f.i. QSharedPointer: pointer 0xa10010 already has reference counting to the more "debuggable" (and consistent with non-QObjects) QSharedPointer: internal self-check failed: pointer 0x1dd90e0 was already tracked by another QSharedPointer object 0x1dd9330 "Backport" of change-id I71340d0f878828354537762d01c46d441efc918c from qtbase (although, as of commit 609b0a9c2e69180ceff5d89c42e0d9468491d7e3, this issue is not present there, as setQObjectShared does nothing). Change-Id: I4cea83d705795eae0e05da128a3f189aea36f5ce Reviewed-by: Thiago Macieira --- src/corelib/tools/qsharedpointer_impl.h | 2 +- tests/auto/qsharedpointer/tst_qsharedpointer.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 4216456..95864a1 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -378,10 +378,10 @@ namespace QtSharedPointer { inline void internalFinishConstruction(T *ptr) { Basic::internalConstruct(ptr); - if (ptr) d->setQObjectShared(ptr, true); #ifdef QT_SHAREDPOINTER_TRACK_POINTERS if (ptr) internalSafetyCheckAdd2(d, ptr); #endif + if (ptr) d->setQObjectShared(ptr, true); } inline ExternalRefCount() : d(0) { } diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp index 452e658..54fe65e 100644 --- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp @@ -1755,6 +1755,13 @@ void tst_QSharedPointer::invalidConstructs_data() "QSharedPointer ptr1 = QSharedPointer(aData);\n" "QSharedPointer ptr2 = QSharedPointer(aData);\n"; + // two QObjects with the same pointer + QTest::newRow("same-pointer-to-qobject") + << &QTest::QExternalTest::tryRunFail + << "QObject *anObj = new QObject;\n" + "QSharedPointer ptr1 = QSharedPointer(anObj);\n" + "QSharedPointer ptr2 = QSharedPointer(anObj);\n"; + // re-creation: QTest::newRow("re-creation") << &QTest::QExternalTest::tryRunFail -- cgit v0.12