Lifetime of captured variables in QFuture continuation
-
Suppose I have this class
class MyClass { public: MyClass(); ~MyClass(); QFuture<void> m_future; };And I want to do something like this:
{ auto myObject = std::make_shared<MyClass>(); myObject->m_future = QtFuture::connect(this, &TestFutureLifetimeApp::someSignal).then( [myObject]() { qDebug() << "lambda executed"; }); }and assume that the
shared_ptrofmyObjectonly exists in the capture clause of the continuation. When this continuation is ran, willmyObjectdestruct? Seems like the answer is yes based on some basic debugging output:"MyClass::MyClass()" "lambda executed" "~MyClass::MyClass()"but I wanted to double check.
I tried to get this info from the repo, but the closest I could find was QFutureInterfaceBase::runContinuation(), which mentions a concept of
QFutureInterfaceBasePrivate::Cleaned. -
B brno4758 deleted this topic on
-
B brno4758 restored this topic on
-
Hi and welcome to devnet,
Your myObject shared pointer is captured by the lambda and since it's not shared anywhere else, once the lambda has run, it will be destroyed.
-
B brno4758 has marked this topic as solved on
-
Hi and welcome to devnet,
Your myObject shared pointer is captured by the lambda and since it's not shared anywhere else, once the lambda has run, it will be destroyed.