From 95cea24fa2f3eed5e7d8cfca085a6f09e7643331 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Mon, 30 Nov 2020 13:03:41 +0100 Subject: Fix memory leaks in QFuture's continuations There were two issues: - Some of the continuations were allocating memory for the continuation's context dynamically, but deleting the allocated memory only if they were actually invoked. Since the continuations may not be invoked at all, this could cause memory leaks. Fixed by postponing the allocations to the point when the continuations need to be invoked. - In other cases the parent future is captured by copy in the continuation's lambda, which is then saved in the parent. This causes the following problem: the data of the ref-counted parent will be deleted as soon as its last copy gets deleted. But the saved continuation will prevent it from being deleted, since it holds a copy of parent. To break the circular dependency, instead of capturing the parent inside the lambda, we can pass the parent's data directly to continuation when calling it. Fixes: QTBUG-87289 Change-Id: If340520b68f6e960bc80953ca18b796173d34f7b Reviewed-by: Andrei Golubev Reviewed-by: Ivan Solovev Reviewed-by: Karsten Heimrich (cherry picked from commit 5d26d40a5596be048be87f309df9264bac741be9) Reviewed-by: Jarek Kobus --- src/corelib/thread/qfutureinterface.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/corelib/thread/qfutureinterface.cpp') diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp index 57ddc7407e..7a8ba08103 100644 --- a/src/corelib/thread/qfutureinterface.cpp +++ b/src/corelib/thread/qfutureinterface.cpp @@ -744,14 +744,14 @@ void QFutureInterfaceBasePrivate::setState(QFutureInterfaceBase::State newState) state.storeRelaxed(newState); } -void QFutureInterfaceBase::setContinuation(std::function func) +void QFutureInterfaceBase::setContinuation(std::function func) { QMutexLocker lock(&d->continuationMutex); // If the state is ready, run continuation immediately, // otherwise save it for later. if (isFinished()) { lock.unlock(); - func(); + func(*this); } else { d->continuation = std::move(func); } @@ -762,7 +762,7 @@ void QFutureInterfaceBase::runContinuation() const QMutexLocker lock(&d->continuationMutex); if (d->continuation) { lock.unlock(); - d->continuation(); + d->continuation(*this); } } -- cgit v1.2.3