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

    Hi!
    Made a simple test:

    void mythread::run(){
        while(work){
            bool boolRESULT;
            QString output_str;
            QByteArray output;
            QStringList output_strlst;
            QTextCodec *codec;
            QString exe_path = "/bin/ping";;
            QStringList arguments;
            arguments << "127.0.0.1" << "-c" << "1";
    
            if(myProcess == nullptr) myProcess = new QProcess;
            myProcess->start(exe_path, arguments);
            myProcess->waitForFinished(500);
    
            output = myProcess->readAll();
            output_str = codec->toUnicode(output);
            output_strlst = output_str.split("\r\n");
    
            myProcess->close();
             
            boolRESULT = false;
            for (int i = 0; i < output_strlst.count(); i++){
                boolRESULT = output_strlst[i].contains("ttl",Qt::CaseInsensitive);
                if (boolRESULT) break;
            }
            emit setStatus(boolRESULT);
       
            msleep(1000);
        }
    }
    

    Valgrind output:
    ...
    ==23633== 2,400 bytes in 15 blocks are still reachable in loss record 2,049 of 2,114
    ==23633== at 0x4C2BBEF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==23633== by 0x6267251: resizeSignalVector (qobject_p.h:297)
    ==23633== by 0x6267251: QObjectPrivate::addConnection(int, QObjectPrivate::Connection*) (qobject.cpp:330)
    ==23633== by 0x6268A18: QMetaObjectPrivate::connect(QObject const*, int, QMetaObject const*, QObject const*, int, QMetaObject const*, int, int*) (qobject.cpp:3453)
    ==23633== by 0x626FD58: QObject::connect(QObject const*, char const*, QObject const*, char const*, Qt::ConnectionType) (qobject.cpp:2911)
    ==23633== by 0x61BFB1F: QProcessPrivate::startProcess() (qprocess_unix.cpp:384)
    ==23633== by 0x61BA34D: QProcessPrivate::start(QFlagsQIODevice::OpenModeFlag) (qprocess.cpp:2246)
    ==23633== by 0x61BA582: QProcess::start(QString const&, QStringList const&, QFlagsQIODevice::OpenModeFlag) (qprocess.cpp:2094)
    ==23633== by 0x10BBBF: mythread::run() (mythread.cpp:19)

    ==23633== by 0x605A3B4: QThreadPrivate::start(void*) (qthread_unix.cpp:342)
    ==23633== by 0x6A164A3: start_thread (pthread_create.c:456)
    ==23633== by 0x75B1D0E: clone (clone.S:97)
    ...

    mythread.cpp 19 line is:

    myProcess->start(exe_path, arguments);
    

    My log by system monitor:

    • 16:50 4300 KB
    • 17:06 4790 KB
    • 17:20 5200 KB
    • 17:30 5552 KB

    Any ideas?

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

    @sitesv
    In addition, from @KroMignon no QEventLoop also means no deleting/disposing/freeing of Qt objects.

    On a side note: nothing to do with your findings, but for the record

     arguments << "127.0.0.1" << "-c 1";
    

    is wrong: -c & 1 are separate arguments, it ought to be << "-c" << "1". You are lucky it works as one argument.

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

      @KroMignon @JonB
      Are there any methods to do this correctly?
      If I do this in the main thread: the app will freeze while QProcess execution - this is unacceptable for me.

      KroMignonK 1 Reply Last reply
      0
      • sitesvS sitesv

        @KroMignon @JonB
        Are there any methods to do this correctly?
        If I do this in the main thread: the app will freeze while QProcess execution - this is unacceptable for me.

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

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

        Are there any methods to do this correctly?

        I don't say this is incorrect.
        I depends what you want to achieve.
        If you don't need an event queue, it works. If you are using signals/slots, this will not work.

        I do not know what is the purpose of this code extract, so maybe it is the best way to do.

        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:

          Are there any methods to do this correctly?

          I don't say this is incorrect.
          I depends what you want to achieve.
          If you don't need an event queue, it works. If you are using signals/slots, this will not work.

          I do not know what is the purpose of this code extract, so maybe it is the best way to do.

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

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

          If you don't need an event queue, it works. If you are using signals/slots, this will not work.

          I don't know, but from this qthread in my main app I emit signal... and it is emitted... slot is from the main thread. QThread starts by start() method.
          The problem for me is memory leak only.

          KroMignonK 1 Reply Last reply
          0
          • JonBJ JonB

            @sitesv
            In addition, from @KroMignon no QEventLoop also means no deleting/disposing/freeing of Qt objects.

            On a side note: nothing to do with your findings, but for the record

             arguments << "127.0.0.1" << "-c 1";
            

            is wrong: -c & 1 are separate arguments, it ought to be << "-c" << "1". You are lucky it works as one argument.

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

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

            In addition, from @KroMignon no QEventLoop also means no deleting/disposing/freeing of Qt objects.

            QCoreApplication::processEvents would help?

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

            is wrong: -c & 1 are separate arguments, it ought to be << "-c" << "1". You are lucky it works as one argument.

            Thank you! Fixed.

            1 Reply Last reply
            0
            • sitesvS sitesv

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

              If you don't need an event queue, it works. If you are using signals/slots, this will not work.

              I don't know, but from this qthread in my main app I emit signal... and it is emitted... slot is from the main thread. QThread starts by start() method.
              The problem for me is memory leak only.

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

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

              I don't know, but from this qthread in my main app I emit signal... and it is emitted... slot is from the main thread. QThread starts by start() method.
              The problem for me is memory leak only.

              For my comprehension of QObject and QThread (cf Threads and QObjects), your implementation is not really Qt friendly.

              If I right understand what you want to achieve: you want to run "ping" a regular interval and be informed when ping has failed.

              To do this, I would create a QObject class which handles the ping requests and eventually moves this class instance to a dedicated thread to avoid lock on main thread.

              Something like:

              class PingTester : public QObject
              {
                  Q_OBJECT
              
              public:
                  explicit PingTester(QObject* parent = nullptr): QObject(parent), m_timer(new QTimer(this))
                  {
                      m_timer->setInterval(30*1000); // per default 30 seconds
                      m_timer->setSingleShot(false);
                      connect(m_timer, &QTimer::timeout, this, &PingTester::doPing);
                  }
              
                  void setInterval(int interval) { m_timer->setInterval(interval); }
              
              public slots:
                   void start()
                   {
                       if(QThread::currentThrea() != thread())
                       {
                            QTimer::singleShot(this, 0, &PingTester::start);
                            return;
                       }
                       if(!m_timer->isActive())
                           m_timer->start();
                   }
                   void stop()
                   {
                       if(QThread::currentThrea() != thread())
                       {
                            QTimer::singleShot(this, 0, &PingTester::stop);
                            return;
                       }
                       if(m_timer->isActive())
                           m_timer->stop();
                   }
                   
              private slots:
                  void doPing();
              
              signals:
                  void pingFailure();
                  void pingSuccess();
              
              private:
                   QTimer* m_timer;
              };
              

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

                Hi,

                Can you explain why exactly do you need that ping ?

                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
                1
                • sitesvS Offline
                  sitesvS Offline
                  sitesv
                  wrote on last edited by
                  #26

                  @SGaist
                  ping needs to check the availability of pc from the internal network. I thought that this is the simplest 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
                    #27

                    But why a thread then?

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

                                          • Login

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