summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorOrgad Shaneh <orgad.shaneh@audiocodes.com>2012-11-04 19:21:31 (GMT)
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-11-08 14:11:56 (GMT)
commit17bea1689abc695d88f13cd15f73b0a59fcffdff (patch)
tree3686d3b4face64fb47fdb643a8bb73c887481101 /tests
parenta4b5cd2893a5eef09d615340ae899f785de84858 (diff)
downloadQt-17bea1689abc695d88f13cd15f73b0a59fcffdff.zip
Qt-17bea1689abc695d88f13cd15f73b0a59fcffdff.tar.gz
Qt-17bea1689abc695d88f13cd15f73b0a59fcffdff.tar.bz2
QtConcurrent: Fix for leak in QFuture
To avoid leaking when converting a QFuture<T> to a QFuture<void> we need to have a separate ref. counter for QFuture<T>. When the last QFuture<T> goes out of scope, we need to clean out the result data. backported from qt/qtbase commit 731ba8ed08f80644b403556638c7f6229e678ebe Original commit by Christian Strømme Task-number: QTBUG-27224 Change-Id: I0c6b525cf241b5c559a1bab4e0066cd4de556ea8 Reviewed-by: Christian Stromme <christian.stromme@digia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qfuture/tst_qfuture.cpp30
-rw-r--r--tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp43
2 files changed, 65 insertions, 8 deletions
diff --git a/tests/auto/qfuture/tst_qfuture.cpp b/tests/auto/qfuture/tst_qfuture.cpp
index f6ed5a9..0f1836c 100644
--- a/tests/auto/qfuture/tst_qfuture.cpp
+++ b/tests/auto/qfuture/tst_qfuture.cpp
@@ -1282,18 +1282,32 @@ void tst_QFuture::throttling()
void tst_QFuture::voidConversions()
{
- QFutureInterface<int> iface;
- iface.reportStarted();
+ {
+ QFutureInterface<int> iface;
+ iface.reportStarted();
- QFuture<int> intFuture(&iface);
+ QFuture<int> intFuture(&iface);
+ int value = 10;
+ iface.reportFinished(&value);
- int value = 10;
- iface.reportFinished(&value);
+ QFuture<void> voidFuture(intFuture);
+ voidFuture = intFuture;
+
+ QVERIFY(voidFuture == intFuture);
+ }
- QFuture<void> voidFuture(intFuture);
- voidFuture = intFuture;
+ {
+ QFuture<void> voidFuture;
+ {
+ QFutureInterface<QList<int> > iface;
+ iface.reportStarted();
- QVERIFY(voidFuture == intFuture);
+ QFuture<QList<int> > listFuture(&iface);
+ iface.reportResult(QList<int>() << 1 << 2 << 3);
+ voidFuture = listFuture;
+ }
+ QCOMPARE(voidFuture.resultCount(), 0);
+ }
}
diff --git a/tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp
index 43075f1..f8c4a36 100644
--- a/tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp
+++ b/tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp
@@ -43,6 +43,7 @@
#include <qdebug.h>
#include <QThread>
+#include <QMutex>
#include <QtTest/QtTest>
@@ -76,6 +77,7 @@ private slots:
void stlContainers();
void qFutureAssignmentLeak();
void stressTest();
+ void persistentResultTest();
public slots:
void throttling();
};
@@ -2416,6 +2418,47 @@ 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<void> voidFuture;
+ QMutex mtx;
+ QAtomicInt ref;
+ LockedCounter lc(&mtx, &ref);
+ QList<int> list;
+ {
+ list << 1 << 2 << 3;
+ mtx.lock();
+ QFuture<int> future = QtConcurrent::mapped(list
+ ,lc);
+ voidFuture = future;
+ }
+ QCOMPARE(int(ref), 0);
+ mtx.unlock(); // Unblock
+ voidFuture.waitForFinished();
+ QCOMPARE(int(ref), 3);
+}
+
QTEST_MAIN(tst_QtConcurrentMap)
#else