Fix memory leak in QtConcurrent::run when called with a NULL QThreadPool
QThreadPool automatically deletes the runnable after it finishes running the task. In case QThreadPool is nullptr, we should delete the runnable manually. This amends 87b93c29be02f0a7ff9424b5e2b6431e20bd4c40. Pick-to: 6.3 6.2 5.15 Change-Id: Id7e4ed3d4d6de05990edf62e4099852983debc64 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
df0f8e036c
commit
4cd8eeaf8c
@ -99,6 +99,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
promise.reportCanceled();
|
promise.reportCanceled();
|
||||||
promise.reportFinished();
|
promise.reportFinished();
|
||||||
|
delete this;
|
||||||
}
|
}
|
||||||
return theFuture;
|
return theFuture;
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ private slots:
|
|||||||
void customPromise();
|
void customPromise();
|
||||||
void nonDefaultConstructibleValue();
|
void nonDefaultConstructibleValue();
|
||||||
void nullThreadPool();
|
void nullThreadPool();
|
||||||
|
void nullThreadPoolNoLeak();
|
||||||
};
|
};
|
||||||
|
|
||||||
void light()
|
void light()
|
||||||
@ -1589,5 +1590,27 @@ void tst_QtConcurrentRun::nullThreadPool()
|
|||||||
QVERIFY(!isInvoked);
|
QVERIFY(!isInvoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct LifetimeChecker
|
||||||
|
{
|
||||||
|
LifetimeChecker() { ++count; }
|
||||||
|
LifetimeChecker(const LifetimeChecker &) { ++count; }
|
||||||
|
~LifetimeChecker() { --count; }
|
||||||
|
|
||||||
|
void operator()() { }
|
||||||
|
|
||||||
|
static std::atomic<int> count;
|
||||||
|
};
|
||||||
|
std::atomic<int> LifetimeChecker::count = 0;
|
||||||
|
|
||||||
|
void tst_QtConcurrentRun::nullThreadPoolNoLeak()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
QThreadPool *pool = nullptr;
|
||||||
|
auto future = run(pool, LifetimeChecker());
|
||||||
|
future.waitForFinished();
|
||||||
|
}
|
||||||
|
QCOMPARE(LifetimeChecker::count, 0);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(tst_QtConcurrentRun)
|
QTEST_MAIN(tst_QtConcurrentRun)
|
||||||
#include "tst_qtconcurrentrun.moc"
|
#include "tst_qtconcurrentrun.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user