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. Executing QProcess in QThread: memory leak
Forum Updated to NodeBB v4.3 + New Features

Executing QProcess in QThread: memory leak

Scheduled Pinned Locked Moved Unsolved General and Desktop
79 Posts 7 Posters 22.3k Views 2 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.
  • sitesvS sitesv

    @SGaist code updated

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by
    #40

    @sitesv For example a possible implementation which will not lock the event loop could be:

    void PingTester::doPing()
    {		
        QProcess ping;
        QEventLoop l; // used for passive wait until process finished
    
        ping.setProcessChannelMode(QProcess::MergedChannels);
        connect(&ping, &QProcess::finished, 
                [this, &l, &ping]() {
                    QString output(ping.readAll());
                    if(output.contains("ttl",Qt::CaseInsensitive))
                        emit pingSuccess();
                    else
                        emit pingFailure();
                    // exit event loop on process end
                    l.exit();
                });
        // starting ping request
        ping.start("/bin/ping", QStringList() << "127.0.0.1" << "-c" << "1");
    
        // wait until ping finished
        l.exec();
    
        // start next ping (timer should be configured as single shot)
        m_timer->start();
    }
    

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    sitesvS 1 Reply Last reply
    1
    • KroMignonK KroMignon

      @sitesv For example a possible implementation which will not lock the event loop could be:

      void PingTester::doPing()
      {		
          QProcess ping;
          QEventLoop l; // used for passive wait until process finished
      
          ping.setProcessChannelMode(QProcess::MergedChannels);
          connect(&ping, &QProcess::finished, 
                  [this, &l, &ping]() {
                      QString output(ping.readAll());
                      if(output.contains("ttl",Qt::CaseInsensitive))
                          emit pingSuccess();
                      else
                          emit pingFailure();
                      // exit event loop on process end
                      l.exit();
                  });
          // starting ping request
          ping.start("/bin/ping", QStringList() << "127.0.0.1" << "-c" << "1");
      
          // wait until ping finished
          l.exec();
      
          // start next ping (timer should be configured as single shot)
          m_timer->start();
      }
      
      sitesvS Offline
      sitesvS Offline
      sitesv
      wrote on last edited by
      #41

      @KroMignon Thank you for the new experience.
      There is a problem with 'connect'

      KroMignonK 1 Reply Last reply
      0
      • sitesvS sitesv

        @KroMignon Thank you for the new experience.
        There is a problem with 'connect'

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #42

        @sitesv said in Executing QProcess in QThread: memory leak:

        There is a problem with 'connect'

        Can you detail more the problem?

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        sitesvS 1 Reply Last reply
        0
        • KroMignonK KroMignon

          @sitesv said in Executing QProcess in QThread: memory leak:

          There is a problem with 'connect'

          Can you detail more the problem?

          sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by
          #43

          @KroMignon said in Executing QProcess in QThread: memory leak:

          Can you detail more the problem?

          pingtester.cpp:9:5: error: no matching member function for call to 'connect'
          qobject.h:481:41: note: candidate function not viable: no overload of 'finished' matching 'const char *' for 2nd argument
          qobject.h:274:13: note: candidate template ignored: couldn't infer template argument 'Func1'
          qobject.h:314:13: note: candidate template ignored: couldn't infer template argument 'Func1'
          qobject.h:242:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
          qobject.h:283:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
          qobject.h:322:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
          qobject.h:222:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
          qobject.h:225:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided

          KroMignonK 1 Reply Last reply
          0
          • sitesvS sitesv

            @KroMignon said in Executing QProcess in QThread: memory leak:

            Can you detail more the problem?

            pingtester.cpp:9:5: error: no matching member function for call to 'connect'
            qobject.h:481:41: note: candidate function not viable: no overload of 'finished' matching 'const char *' for 2nd argument
            qobject.h:274:13: note: candidate template ignored: couldn't infer template argument 'Func1'
            qobject.h:314:13: note: candidate template ignored: couldn't infer template argument 'Func1'
            qobject.h:242:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
            qobject.h:283:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
            qobject.h:322:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
            qobject.h:222:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
            qobject.h:225:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #44

            @sitesv Oh, my bad, QProcess::finished() has an overload (https://doc.qt.io/qt-5/qprocess.html#finished)

            Should be changed to:

            connect(&ping, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), 
                        [this, &l, &ping]() {
                            QString output(ping.readAll());
                            if(output.contains("ttl",Qt::CaseInsensitive))
                                emit pingSuccess();
                            else
                                emit pingFailure();
                            // exit event loop on process end
                            l.exit();
                        }); 
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by
              #45

              @KroMignon
              Very interesting realization. I will study it.
              Thank you again!

              1 Reply Last reply
              0
              • sitesvS Offline
                sitesvS Offline
                sitesv
                wrote on last edited by sitesv
                #46

                @KroMignon
                Hi!
                Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.

                KroMignonK 1 Reply Last reply
                0
                • sitesvS sitesv

                  @KroMignon
                  Hi!
                  Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #47

                  @sitesv said in Executing QProcess in QThread: memory leak:

                  Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.

                  Yes, it is possible.
                  I have give you a basic implementation example.
                  You just have to complete it.

                  If you want to learn lambda, search on internet, there are many explanations. It is not so hard to understand.
                  My favorite is https://blog.feabhas.com/2014/03/demystifying-c-lambdas/

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  sitesvS 1 Reply Last reply
                  3
                  • KroMignonK KroMignon

                    @sitesv said in Executing QProcess in QThread: memory leak:

                    Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.

                    Yes, it is possible.
                    I have give you a basic implementation example.
                    You just have to complete it.

                    If you want to learn lambda, search on internet, there are many explanations. It is not so hard to understand.
                    My favorite is https://blog.feabhas.com/2014/03/demystifying-c-lambdas/

                    sitesvS Offline
                    sitesvS Offline
                    sitesv
                    wrote on last edited by
                    #48

                    @KroMignon said in Executing QProcess in QThread: memory leak:

                    Yes, it is possible.

                    I have tried to do something like this. But is execute too slow.

                    QStringList ip_list = {"192.168.0.1", "192.168.0.2"};
                        int success_count = 0;
                        foreach(auto ip, ip_list)
                        {
                            QProcess ping;
                            QEventLoop l;
                            ping.setProcessChannelMode(QProcess::MergedChannels);
                            connect(&ping,
                                    QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                    [&l, &ping, &success_count]() {
                                        QString output(ping.readAll());
                                        if(output.contains("ttl",Qt::CaseInsensitive)) success_count++;
                                        l.exit();
                                    });
                            ping.start("/bin/ping", QStringList() << ip << "-c" << "1");
                            l.exec();
                        }
                        if(success_count == ip_list.count())
                           emit setStatus(true);
                        else
                           emit setStatus(false);
                        m_timer->start();
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • sitesvS sitesv

                      @KroMignon said in Executing QProcess in QThread: memory leak:

                      Yes, it is possible.

                      I have tried to do something like this. But is execute too slow.

                      QStringList ip_list = {"192.168.0.1", "192.168.0.2"};
                          int success_count = 0;
                          foreach(auto ip, ip_list)
                          {
                              QProcess ping;
                              QEventLoop l;
                              ping.setProcessChannelMode(QProcess::MergedChannels);
                              connect(&ping,
                                      QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                      [&l, &ping, &success_count]() {
                                          QString output(ping.readAll());
                                          if(output.contains("ttl",Qt::CaseInsensitive)) success_count++;
                                          l.exit();
                                      });
                              ping.start("/bin/ping", QStringList() << ip << "-c" << "1");
                              l.exec();
                          }
                          if(success_count == ip_list.count())
                             emit setStatus(true);
                          else
                             emit setStatus(false);
                          m_timer->start();
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #49
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • sitesvS Offline
                        sitesvS Offline
                        sitesv
                        wrote on last edited by sitesv
                        #50

                        UPD:
                        I found the cause of this situation. Ip's are not available. How to set a timeout for the process execution in this case?

                        jsulmJ KroMignonK 2 Replies Last reply
                        0
                        • sitesvS sitesv

                          UPD:
                          I found the cause of this situation. Ip's are not available. How to set a timeout for the process execution in this case?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by jsulm
                          #51

                          @sitesv Why do you actually use a local event loop? You can implement this functionality without blocking your app with a local event loop.
                          Also, you terminate that event loop already in the first lambda call (when the first process finishes)...

                          sitesvS 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @sitesv Why do you actually use a local event loop? You can implement this functionality without blocking your app with a local event loop.
                            Also, you terminate that event loop already in the first lambda call (when the first process finishes)...

                            sitesvS Offline
                            sitesvS Offline
                            sitesv
                            wrote on last edited by
                            #52

                            @jsulm Local EventLoop was recommended before. Could you please advise something?

                            jsulmJ 1 Reply Last reply
                            0
                            • sitesvS sitesv

                              @jsulm Local EventLoop was recommended before. Could you please advise something?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #53

                              @sitesv You know how many processes you started. So, count how many processes already finished (inside the lambda). And as soon as all processes finished you can check success_count and emit the signal. All this can be done inside lambda.

                              1 Reply Last reply
                              0
                              • sitesvS sitesv

                                UPD:
                                I found the cause of this situation. Ip's are not available. How to set a timeout for the process execution in this case?

                                KroMignonK Offline
                                KroMignonK Offline
                                KroMignon
                                wrote on last edited by
                                #54

                                @sitesv said in Executing QProcess in QThread: memory leak:

                                How to set a timeout for the process execution in this case?

                                Have you tried to look at ping parameters (ping --help)

                                And I would only wait once, for all pings to be done:

                                QStringList ip_list = {"192.168.0.1", "192.168.0.2"};
                                int success_count = 0;
                                int pingsToDo = ip_list.count();
                                QEventLoop l;
                                foreach(auto ip, ip_list)
                                {
                                    QProcess ping;
                                    ping.setProcessChannelMode(QProcess::MergedChannels);
                                    connect(&ping,
                                            QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                            [&l, &ping, &success_count, &pingsToDo]() {
                                                --pingsToDo;
                                                QString output(ping.readAll());
                                                if(output.contains("ttl",Qt::CaseInsensitive)) success_count++;
                                                if(!pingsToDo)
                                                    l.exit();
                                            });
                                    ping.start("/bin/ping", QStringList() << ip << "-c" << "1");
                                }
                                
                                // wait all pings done
                                l.exec();
                                if(success_count == ip_list.count())
                                   emit setStatus(true);
                                else
                                   emit setStatus(false);
                                m_timer->start();
                                

                                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                sitesvS 1 Reply Last reply
                                1
                                • KroMignonK KroMignon

                                  @sitesv said in Executing QProcess in QThread: memory leak:

                                  How to set a timeout for the process execution in this case?

                                  Have you tried to look at ping parameters (ping --help)

                                  And I would only wait once, for all pings to be done:

                                  QStringList ip_list = {"192.168.0.1", "192.168.0.2"};
                                  int success_count = 0;
                                  int pingsToDo = ip_list.count();
                                  QEventLoop l;
                                  foreach(auto ip, ip_list)
                                  {
                                      QProcess ping;
                                      ping.setProcessChannelMode(QProcess::MergedChannels);
                                      connect(&ping,
                                              QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                              [&l, &ping, &success_count, &pingsToDo]() {
                                                  --pingsToDo;
                                                  QString output(ping.readAll());
                                                  if(output.contains("ttl",Qt::CaseInsensitive)) success_count++;
                                                  if(!pingsToDo)
                                                      l.exit();
                                              });
                                      ping.start("/bin/ping", QStringList() << ip << "-c" << "1");
                                  }
                                  
                                  // wait all pings done
                                  l.exec();
                                  if(success_count == ip_list.count())
                                     emit setStatus(true);
                                  else
                                     emit setStatus(false);
                                  m_timer->start();
                                  
                                  sitesvS Offline
                                  sitesvS Offline
                                  sitesv
                                  wrote on last edited by sitesv
                                  #55

                                  @KroMignon said in Executing QProcess in QThread: memory leak:

                                  And I would only wait once, for all pings to be done:

                                  Wow! Very nice!! Thank you!

                                  @KroMignon
                                  UPD: app is freezing on line l.exec() althought l.exit() is done....

                                  1 Reply Last reply
                                  0
                                  • sitesvS Offline
                                    sitesvS Offline
                                    sitesv
                                    wrote on last edited by
                                    #56

                                    Guys, how to resolve this?

                                    KroMignonK 1 Reply Last reply
                                    0
                                    • sitesvS sitesv

                                      Guys, how to resolve this?

                                      KroMignonK Offline
                                      KroMignonK Offline
                                      KroMignon
                                      wrote on last edited by
                                      #57

                                      @sitesv said in Executing QProcess in QThread: memory leak:

                                      Guys, how to resolve this?

                                      Hmm, I think it is a tricky issue. I would start with forcing QueuedConnection, to avoid threading issues:

                                      connect(&ping,
                                                  QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                                  [&l, &ping, &success_count, &pingsToDo]() {
                                                      ...
                                                  }, Qt::QueuedConnection);
                                      

                                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                      sitesvS 1 Reply Last reply
                                      0
                                      • KroMignonK KroMignon

                                        @sitesv said in Executing QProcess in QThread: memory leak:

                                        Guys, how to resolve this?

                                        Hmm, I think it is a tricky issue. I would start with forcing QueuedConnection, to avoid threading issues:

                                        connect(&ping,
                                                    QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                                    [&l, &ping, &success_count, &pingsToDo]() {
                                                        ...
                                                    }, Qt::QueuedConnection);
                                        
                                        sitesvS Offline
                                        sitesvS Offline
                                        sitesv
                                        wrote on last edited by
                                        #58

                                        @KroMignon said in Executing QProcess in QThread: memory leak:

                                        connect(&ping,
                                        QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                        &l, &ping, &success_count, &pingsToDo {
                                        ...
                                        }, Qt::QueuedConnection);

                                        'connect' failed again
                                        img1.png

                                        KroMignonK 2 Replies Last reply
                                        0
                                        • sitesvS sitesv

                                          @KroMignon said in Executing QProcess in QThread: memory leak:

                                          connect(&ping,
                                          QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                          &l, &ping, &success_count, &pingsToDo {
                                          ...
                                          }, Qt::QueuedConnection);

                                          'connect' failed again
                                          img1.png

                                          KroMignonK Offline
                                          KroMignonK Offline
                                          KroMignon
                                          wrote on last edited by KroMignon
                                          #59

                                          @sitesv Can you show the code you have written

                                          Sorry my fault, should be:

                                          connect(&ping,
                                                  QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                                  &l, // receiver!
                                                  [&l, &ping, &success_count, &pingsToDo]() {
                                                      ...
                                                  }, Qt::QueuedConnection);
                                          

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          sitesvS 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