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. QtConcurrent::run with objects that are deleted
Qt 6.11 is out! See what's new in the release blog

QtConcurrent::run with objects that are deleted

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.5k Views 3 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.
  • V Offline
    V Offline
    Vadi2
    wrote on last edited by
    #1

    I have the following code:

    auto future = QtConcurrent::run(this, &XMLexport::saveXml, fileName);
    auto watcher = new QFutureWatcher<bool>;
    QObject::connect(watcher, &QFutureWatcher<bool>::finished, [=]() { mpHost->xmlSaved(fileName); });
    watcher->setFuture(future);
    

    If my QFutureWatcher goes off when mpHost (a QPointer) is already gone, bad things happen. What would be the best way of dealing with this? I don't care about calling xmlSaved() if the mpHost is already gone.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Vadi2 said in QtConcurrent::run with objects that are deleted:

      [=]() { if (mpHost) mpHost->xmlSaved(fileName); }
      

      (Z(:^

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vadi2
        wrote on last edited by
        #3

        First thing I tried! It didn't work; I get a crash deep inside Qt then even though debugger says host is 0x0.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Yeah, thought that would be too easy... :/

          (Z(:^

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Ah, try capturing by ref:

            [&]() { if (mpHost) mpHost->xmlSaved(fileName); }
            

            Since = captures by value, it may be that it copies the pointer value at the time you call connect() (so when object is still alive), which is no longer true later. Capturing by reference should work better.

            (Z(:^

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Vadi2 said in QtConcurrent::run with objects that are deleted:

              QObject::connect(watcher, &QFutureWatcher<bool>::finished, [=]() { mpHost->xmlSaved(fileName); });

              That's the reason why you should avoid the connect() with three params...

              QObject::connect(watcher, &QFutureWatcher<bool>::finished, mpHost, [=]() { mpHost->xmlSaved(fileName); });

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • V Offline
                V Offline
                Vadi2
                wrote on last edited by
                #7

                That worked, but I'm not sure why and what does it mean for the mphost. Is there documentation I can read on this?

                I've already read https://github.com/KDE/clazy/blob/master/docs/checks/README-connect-3arg-lambda.md but it does not explain it well

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by kshegunov
                  #8

                  Use the doc Luke ;-)

                  Short version: the fourth third parameter is a "context" if the context gets delete, then then the disconnection follows and therefore in your case the lambda will not get executed.

                  [Typo fixed ~kshegunov]

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

                  1 Reply Last reply
                  2

                  • Login

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