From 497622cafe235eadb5dd5056b196d8451ee89071 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 23 Dec 2011 17:13:05 +0100 Subject: Make it possible to put QObject tracked with QWeakPointer inside QSharedPointer Do that by keeping the QWeakPointer that track QObject independent of the ones that track QSharedPointer. QSharedPointer do not touch the sharedRefCount in QObjectPrivate anymore When converting a QWeakPointer constructed from a QObject to a QSharedPointer, it will display a warning saying one should not do that. Task-number: QTBUG-22622 Change-Id: I3595e3e7401702410776c458687ab357ad9366ab Reviewed-by: Bradley T. Hughes Reviewed-by: Stephen Kelly Reviewed-by: Olivier Goffart --- src/corelib/tools/qsharedpointer.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/corelib/tools/qsharedpointer.cpp') diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index ee0aee2399..77c3d1e2cb 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -1226,24 +1226,22 @@ QT_BEGIN_NAMESPACE /*! \internal This function is called for a just-created QObject \a obj, to enable - the use of QSharedPointer and QWeakPointer. + the use of QSharedPointer and QWeakPointer in the future. + */ +void QtSharedPointer::ExternalRefCountData::setQObjectShared(const QObject *, bool) +{} - When QSharedPointer is active in a QObject, the object must not be deleted - directly: the lifetime is managed by the QSharedPointer object. In that case, - the deleteLater() and parent-child relationship in QObject only decrease - the strong reference count, instead of deleting the object. +/*! + \internal + This function is called when a QSharedPointer is created from a QWeakPointer + + We check that the QWeakPointer was really created from a QSharedPointer, and + not from a QObject. */ -void QtSharedPointer::ExternalRefCountData::setQObjectShared(const QObject *obj, bool) +void QtSharedPointer::ExternalRefCountData::checkQObjectShared(const QObject *) { - Q_ASSERT(obj); - QObjectPrivate *d = QObjectPrivate::get(const_cast(obj)); - - if (d->sharedRefcount.load() != 0) - qFatal("QSharedPointer: pointer %p already has reference counting", obj); - d->sharedRefcount.store(this); - - // QObject decreases the refcount too, so increase it up - weakref.ref(); + if (strongref.load() < 0) + qWarning("QSharedPointer: cannot create a QSharedPointer from a QObject-tracking QWeakPointer"); } QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::getAndRef(const QObject *obj) -- cgit v1.2.3