From 731ba8ed08f80644b403556638c7f6229e678ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Thu, 25 Oct 2012 12:32:52 +0200 Subject: Fix for leak in QFuture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid leaking when converting a QFuture to a QFuture we need to have a separate ref. counter for QFuture. When the last QFuture goes out of scope, we need to clean out the result data. Task-number: QTBUG-27224 Change-Id: I965a64a11fffbb191ab979cdd030a9aafd4436c2 Reviewed-by: Jędrzej Nowacki --- .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'tests/auto/concurrent') diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index c50e3839b5..b6bc5b085b 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -43,6 +43,7 @@ #include #include +#include #include @@ -75,6 +76,7 @@ private slots: void stlContainers(); void qFutureAssignmentLeak(); void stressTest(); + void persistentResultTest(); public slots: void throttling(); }; @@ -2395,5 +2397,46 @@ void tst_QtConcurrentMap::stressTest() } } +struct LockedCounter +{ + LockedCounter(QMutex *mutex, QAtomicInt *ai) + : mtx(mutex), + ref(ai) {} + + typedef int result_type; + int operator()(int x) + { + QMutexLocker locker(mtx); + ref->ref(); + return ++x; + } + + QMutex *mtx; + QAtomicInt *ref; +}; + +// The Thread engine holds the last reference +// to the QFuture, so this should not leak +// or fail. +void tst_QtConcurrentMap::persistentResultTest() +{ + QFuture voidFuture; + QMutex mtx; + QAtomicInt ref; + LockedCounter lc(&mtx, &ref); + QList list; + { + list << 1 << 2 << 3; + mtx.lock(); + QFuture future = QtConcurrent::mapped(list + ,lc); + voidFuture = future; + } + QCOMPARE(ref.loadAcquire(), 0); + mtx.unlock(); // Unblock + voidFuture.waitForFinished(); + QCOMPARE(ref.loadAcquire(), 3); +} + QTEST_MAIN(tst_QtConcurrentMap) #include "tst_qtconcurrentmap.moc" -- cgit v1.2.3