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 19.0k 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 Offline
    sitesvS Offline
    sitesv
    wrote on last edited by sitesv
    #28

    @KroMignon Thank you for your variant.
    I have some Qs;
    Why you are using QTimer for this purpose?
    doPing() is represents my code with using QProcess?
    This class should create in the main thread?

    KroMignonK 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      But why a thread then?

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

      @Christian-Ehrlicher said in Executing QProcess in QThread: memory leak:

      But why a thread then?

      I need to check availability constantly during the program. I thought the QThread is a good way to do this...

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

        QProcess is asynchronous.

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

        sitesvS 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          QProcess is asynchronous.

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

          @Christian-Ehrlicher said in Executing QProcess in QThread: memory leak:

          QProcess is asynchronous.

          "waitForFinished" is not help?

          Christian EhrlicherC 1 Reply Last reply
          0
          • sitesvS sitesv

            @Christian-Ehrlicher said in Executing QProcess in QThread: memory leak:

            QProcess is asynchronous.

            "waitForFinished" is not help?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #32

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

            "waitForFinished" is not help?

            Why do you want to make a asynchronous object blocking? Please read the QProcess docs and use the proper signals.

            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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #33

              The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

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

              sitesvS 1 Reply Last reply
              1
              • SGaistS SGaist

                The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

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

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

                The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

                I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.

                JonBJ SGaistS 2 Replies Last reply
                0
                • sitesvS sitesv

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

                  The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

                  I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #35

                  @sitesv
                  As @Christian-Ehrlicher has said. You are using this thread to run a QProcess, and you call waitForFinished() on it. You would be better just running QProcess asynchronously from your main thread, and acting on signals. Then perhaps any thread memory issue will go away.

                  sitesvS 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @sitesv
                    As @Christian-Ehrlicher has said. You are using this thread to run a QProcess, and you call waitForFinished() on it. You would be better just running QProcess asynchronously from your main thread, and acting on signals. Then perhaps any thread memory issue will go away.

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

                    @JonB
                    I will try it.

                    1 Reply Last reply
                    0
                    • sitesvS sitesv

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

                      The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

                      I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #37

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

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

                      The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

                      I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.

                      Well, do not skip that kind of details when asking questions, it can hide the issue you are asking help for and it does not allow people to understand what is really happening.

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

                      sitesvS 1 Reply Last reply
                      0
                      • SGaistS SGaist

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

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

                        The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?

                        I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.

                        Well, do not skip that kind of details when asking questions, it can hide the issue you are asking help for and it does not allow people to understand what is really happening.

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

                        @SGaist code updated

                        KroMignonK 1 Reply Last reply
                        0
                        • sitesvS sitesv

                          @KroMignon Thank you for your variant.
                          I have some Qs;
                          Why you are using QTimer for this purpose?
                          doPing() is represents my code with using QProcess?
                          This class should create in the main thread?

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

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

                          Why you are using QTimer for this purpose?

                          I use QTimer here to make a passive wait. When the interval is reached, the QTimer fires the timeout() event. This will not lock the event queue. QThread::msleep() is an active wait ==> CPU usage for nothing and no signals/slots handling possible for the used thread!

                          doPing() is represents my code with using QProcess?

                          Yes, but I would not use a pointer for the QProcess.

                          This class should create in the main thread?

                          As you like. The QTimer instance has this as parent, moving the PingTester instance to another thread, will also move the QTimer instance to the same thread.

                          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 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

                                          • Login

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