diff --git a/src/corelib/thread/qpromise.h b/src/corelib/thread/qpromise.h index f7abf8babc..0fd7680314 100644 --- a/src/corelib/thread/qpromise.h +++ b/src/corelib/thread/qpromise.h @@ -71,15 +71,9 @@ public: QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPromise) ~QPromise() { - const int state = d.loadState(); - // If QFutureInterface has no state, there is nothing to be done - if (state == static_cast(QFutureInterfaceBase::State::NoState)) { - d.cleanContinuation(); - return; - } - // Otherwise, if computation is not finished at this point, cancel + // If computation is not finished at this point, cancel // potential waits - if (!(state & QFutureInterfaceBase::State::Finished)) { + if (d.d && !(d.loadState() & QFutureInterfaceBase::State::Finished)) { d.cancelAndFinish(); // cancel and finalize the state d.cleanContinuation(); } diff --git a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp index 52f4daa24a..5c20287e89 100644 --- a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp @@ -64,6 +64,7 @@ private slots: void cancelWhenDestroyed(); #endif void cancelWhenReassigned(); + void cancelWhenDestroyedWithoutStarting(); void finishWhenSwapped(); void cancelWhenMoved(); void waitUntilResumed(); @@ -505,6 +506,19 @@ void tst_QPromise::cancelWhenReassigned() #endif } +void tst_QPromise::cancelWhenDestroyedWithoutStarting() +{ + QFuture future; + { + QPromise promise; + future = promise.future(); + } + future.waitForFinished(); + QVERIFY(!future.isStarted()); + QVERIFY(future.isCanceled()); + QVERIFY(future.isFinished()); +} + void tst_QPromise::finishWhenSwapped() { #if !QT_CONFIG(cxx11_future)