summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2009-08-12 09:03:49 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2009-08-12 09:12:36 (GMT)
commit171d92dc8b800b24d28c7b4abf34d6961763bbf4 (patch)
treebcf2ce2ebf6546faa5cc2dc69e01687adb82f947
parentc8563ec79f01ec54a03bb2fdbba11a73a01370f7 (diff)
downloadQt-171d92dc8b800b24d28c7b4abf34d6961763bbf4.zip
Qt-171d92dc8b800b24d28c7b4abf34d6961763bbf4.tar.gz
Qt-171d92dc8b800b24d28c7b4abf34d6961763bbf4.tar.bz2
Autotest: make sure we can't create QWeakPointer from a QObject in destruction.
This test only works in debug mode
-rw-r--r--src/corelib/tools/qsharedpointer.cpp2
-rw-r--r--tests/auto/qsharedpointer/tst_qsharedpointer.cpp15
2 files changed, 17 insertions, 0 deletions
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp
index f793f26..e3e1db6 100644
--- a/src/corelib/tools/qsharedpointer.cpp
+++ b/src/corelib/tools/qsharedpointer.cpp
@@ -977,6 +977,8 @@ QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::ge
{
Q_ASSERT(obj);
QObjectPrivate *d = QObjectPrivate::get(const_cast<QObject *>(obj));
+ Q_ASSERT_X(!d->wasDeleted, "QWeakPointer", "Detected QWeakPointer creation in a QObject being deleted");
+
ExternalRefCountData *that = d->sharedRefcount;
if (that) {
that->weakref.ref();
diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
index 6bc3aa0..58e5401 100644
--- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
+++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
@@ -1593,6 +1593,21 @@ void tst_QSharedPointer::invalidConstructs_data()
<< "QObject *ptr = new QObject;\n"
"QWeakPointer<QObject> weak = ptr;\n" // this makes the object unmanaged
"QSharedPointer<QObject> shared(ptr);\n";
+
+#ifndef QT_NO_DEBUG
+ // this tests a Q_ASSERT, so it is only valid in debug mode
+ // the DerivedFromQObject destructor below creates a QWeakPointer from parent().
+ // parent() is not 0 in the current Qt implementation, but has started destruction,
+ // so the code should detect that issue
+ QTest::newRow("shared-pointer-from-qobject-in-destruction")
+ << &QTest::QExternalTest::tryRunFail
+ << "class DerivedFromQObject: public QObject { public:\n"
+ " DerivedFromQObject(QObject *parent): QObject(parent) {}\n"
+ " ~DerivedFromQObject() { QWeakPointer<QObject> weak = parent(); }\n"
+ "};\n"
+ "QObject obj;\n"
+ "new DerivedFromQObject(&obj);";
+#endif
}
void tst_QSharedPointer::invalidConstructs()