Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Lifetime of captured variables in QFuture continuation

Lifetime of captured variables in QFuture continuation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 283 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brno4758
    wrote on last edited by
    #1

    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_ptr of myObject only exists in the capture clause of the continuation. When this continuation is ran, will myObject destruct? 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.

    1 Reply Last reply
    0
    • B brno4758 deleted this topic on
    • B brno4758 restored this topic on
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • B brno4758 has marked this topic as solved on
      • SGaistS SGaist

        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 Offline
        B Offline
        brno4758
        wrote on last edited by
        #3

        @SGaist Gotcha thanks for the reply

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved