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 87b93c29be.

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:
Sona Kurazyan 2022-01-24 15:57:07 +01:00
parent df0f8e036c
commit 4cd8eeaf8c
2 changed files with 24 additions and 0 deletions

View File

@ -99,6 +99,7 @@ public:
} else {
promise.reportCanceled();
promise.reportFinished();
delete this;
}
return theFuture;
}

View File

@ -66,6 +66,7 @@ private slots:
void customPromise();
void nonDefaultConstructibleValue();
void nullThreadPool();
void nullThreadPoolNoLeak();
};
void light()
@ -1589,5 +1590,27 @@ void tst_QtConcurrentRun::nullThreadPool()
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)
#include "tst_qtconcurrentrun.moc"