From afdae3618a08da9f2c41b7c4ea57219f444ee324 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 18 Jan 2022 13:35:24 +0100 Subject: [PATCH] Create QFutures returned by QtFuture::when* methods via QPromise MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is required to ensure that the continuation attached to a QFuture returned by QtFuture::when* methods is cleaned in the destructor of the associated QPromise, so that it doesn't keep any ref-counted copies to the shared data, thus preventing it from being deleted. Task-number: QTBUG-99534 Pick-to: 6.3 Change-Id: If4e2929b2e638d6b48c95f0aef9dc886066cedbe Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne --- src/corelib/thread/qfuture_impl.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index 112ac21f9e8..69f57652ea0 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -1006,13 +1006,13 @@ struct WhenAllContext futures[index] = std::forward(future); Q_ASSERT(count > 0); if (--count <= 0) { - promise.reportResult(futures); - promise.reportFinished(); + promise.addResult(futures); + promise.finish(); } } QAtomicInteger count; - QFutureInterface promise; + QPromise promise; ResultFutures futures; }; @@ -1025,13 +1025,13 @@ struct WhenAnyContext void checkForCompletion(qsizetype, T &&result) { if (!ready.fetchAndStoreRelaxed(true)) { - promise.reportResult(std::forward(result)); - promise.reportFinished(); + promise.addResult(std::forward(result)); + promise.finish(); } } QAtomicInt ready = false; - QFutureInterface promise; + QPromise promise; }; template @@ -1066,7 +1066,7 @@ QFuture whenAllImpl(InputIt first, InputIt last) auto context = QSharedPointer>::create(size); context->futures.resize(size); - context->promise.reportStarted(); + context->promise.start(); qsizetype idx = 0; for (auto it = first; it != last; ++it, ++idx) { @@ -1085,7 +1085,7 @@ QFuture whenAllImpl(Futures &&... futures) constexpr qsizetype size = sizeof...(Futures); auto context = QSharedPointer>::create(size); context->futures.resize(size); - context->promise.reportStarted(); + context->promise.start(); QtPrivate::addCompletionHandlers(context, std::make_tuple(std::forward(futures)...)); @@ -1106,7 +1106,7 @@ QFuture::type>> whenAnyImpl(I } auto context = QSharedPointer>::create(); - context->promise.reportStarted(); + context->promise.start(); qsizetype idx = 0; for (auto it = first; it != last; ++it, ++idx) { @@ -1125,7 +1125,7 @@ QFuture...>> whenAnyImpl(Futures &&... future using ResultType = std::variant...>; auto context = QSharedPointer>::create(); - context->promise.reportStarted(); + context->promise.start(); QtPrivate::addCompletionHandlers(context, std::make_tuple(std::forward(futures)...));