<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lifetime of captured variables in QFuture continuation]]></title><description><![CDATA[<p dir="auto">Suppose I have this class</p>
<pre><code>class MyClass
{
  public:
    MyClass();
    ~MyClass();
    QFuture&lt;void&gt; m_future;
};
</code></pre>
<p dir="auto">And I want to do something like this:</p>
<pre><code>{
  auto myObject = std::make_shared&lt;MyClass&gt;();
  myObject-&gt;m_future = QtFuture::connect(this, &amp;TestFutureLifetimeApp::someSignal).then(
  [myObject]()
  {
    qDebug() &lt;&lt; "lambda executed";
  });
}
</code></pre>
<p dir="auto">and assume that the <code>shared_ptr</code> of <code>myObject</code> only exists in the capture clause of the continuation. When this continuation is ran, will <code>myObject</code> destruct? Seems like the answer is yes based on some basic debugging output:</p>
<pre><code>"MyClass::MyClass()"
"lambda executed"
"~MyClass::MyClass()"
</code></pre>
<p dir="auto">but I wanted to double check.</p>
<p dir="auto">I tried to get this info from the repo, but the closest I could find was <a href="https://github.com/qt/qtbase/blob/6.6.2/src/corelib/thread/qfutureinterface.cpp#L923" target="_blank" rel="noopener noreferrer nofollow ugc">QFutureInterfaceBase::runContinuation()</a>, which mentions a concept of <code>QFutureInterfaceBasePrivate::Cleaned</code>.</p>
]]></description><link>https://forum.qt.io/topic/157120/lifetime-of-captured-variables-in-qfuture-continuation</link><generator>RSS for Node</generator><lastBuildDate>Mon, 04 May 2026 02:25:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157120.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 07 Jun 2024 19:13:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Lifetime of captured variables in QFuture continuation on Fri, 07 Jun 2024 19:26:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Gotcha thanks for the reply</p>
]]></description><link>https://forum.qt.io/post/802104</link><guid isPermaLink="true">https://forum.qt.io/post/802104</guid><dc:creator><![CDATA[brno4758]]></dc:creator><pubDate>Fri, 07 Jun 2024 19:26:11 GMT</pubDate></item><item><title><![CDATA[Reply to Lifetime of captured variables in QFuture continuation on Fri, 07 Jun 2024 19:19:30 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/802102</link><guid isPermaLink="true">https://forum.qt.io/post/802102</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 07 Jun 2024 19:19:30 GMT</pubDate></item></channel></rss>