From 8c2f0ac3827e765dfd31711ad576b396df7df467 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 20 Sep 2014 18:52:15 +0200 Subject: QObject: fix a memory leak in moveToThread For some reason it seems to be supported to call moveToThread(0). That call will allocate a new QThreadData for the object. However, if we then detect that we're calling moveToThread from a thread which is not the one the QObject has affinity with, we error out leaking that QThreadData object. Change-Id: I0fe6625a2a9887535e457f3897b514d2a03d1480 Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz --- src/corelib/kernel/qobject.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/corelib/kernel/qobject.cpp') diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index e588808ee8..9ab952362c 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -1461,14 +1461,14 @@ void QObject::moveToThread(QThread *targetThread) } QThreadData *currentData = QThreadData::current(); - QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : new QThreadData(0); + QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : Q_NULLPTR; if (d->threadData->thread == 0 && currentData == targetData) { // one exception to the rule: we allow moving objects with no thread affinity to the current thread currentData = d->threadData; } else if (d->threadData != currentData) { qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n" "Cannot move to target thread (%p)\n", - currentData->thread, d->threadData->thread, targetData->thread); + currentData->thread, d->threadData->thread, targetData ? targetData->thread : Q_NULLPTR); #ifdef Q_OS_MAC qWarning("On Mac OS X, you might be loading two sets of Qt binaries into the same process. " @@ -1482,6 +1482,9 @@ void QObject::moveToThread(QThread *targetThread) // prepare to move d->moveToThread_helper(); + if (!targetData) + targetData = new QThreadData(0); + QOrderedMutexLocker locker(¤tData->postEventList.mutex, &targetData->postEventList.mutex); -- cgit v1.2.3