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. Deleting QProcess after finish
QtWS25 Last Chance

Deleting QProcess after finish

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 15.1k Views
  • 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.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    I am creating a process and then call waitForFinished, then delete the process. Problem is I recieve readyReadStandardError signals after waitForFinished and subsequently after I have deleted the process which causes an error when I try and read the output from the now deleted process.

    @
    QProcess * proc = new QProcess();
    // setup args, connect signals, etc..
    proc->start(...);
    proc->waitForFinished(-1);

    delete proc;
    <-- sometime after here readyReadStandardError() signals get emitted from the deleted proc.
    @

    My question is, when can I safely delete the process and still get all of its signals?

    Thanks.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      I haven't verified this but I assume that QProcess emits various signals which aren't processed because QProcess::waitForFinished() blocks your event loop and thus no event handling (signal processing) occours. As soon as QProcess::waitForFinished() returns the events pending in the event loop are processed and the pending signals are emitted, even though your QProcess has been already deleted.

      QProcess offers you two possible interfaces:

      • a synchronous interface using QProcess::waitForFinished(), which means your application is blocked until the executed process is finished
      • an asynchronous interface using appropriate signals (eg. QProcess::finished()), which means your application continues execution and is notified about state changes of the executed process via signals

      There is no sense in mixing both. You either use the synchronous interface (which might lead to a frozen user interface because your event loop is blocked) or the asynchronous interface with signals and slots.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mirswith
        wrote on last edited by
        #3

        The signal queue is also what I am suspecting to be the cause, I suppose I could emit my own signal once waitForFinished() returns and delete the process there. This is of course assuming the signal queue is the culprit and that I would be gauranteed that my emit would occur after anything already in the queue.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          If signals of your process are still pending, you should destroy the object using

          @
          proc->deleteLater();
          @

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mirswith
            wrote on last edited by
            #5

            excellent, this is exactly what I was looking for.

            Thanks!

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              [quote author="mirswith" date="1316459702"]The signal queue is also what I am suspecting to be the cause, I suppose I could emit my own signal once waitForFinished() returns and delete the process there. This is of course assuming the signal queue is the culprit and that I would be gauranteed that my emit would occur after anything already in the queue.[/quote]

              Why do you even connect to signals of QProcess in the first place?

              They have no chance to be delivered, because your thread is suspended until waitForFinished() returns - thus no event processing occours. As soon as waitForFinished() returns it is absolutely safe to process to QProcess output and delete the QProcess (given that no signals have been connected).

              You either use signals or you use waitForFinished(). There is no sense in using both.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mirswith
                wrote on last edited by
                #7

                Actually I am running a number of QProcesses in a QThread which separates them from my GUI thread and I am receiving signals off of them. I am using the signals purely to capture standard/error out messages, however I have not tried to just read them after waitForFinished() which might work just fine.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mirswith
                  wrote on last edited by
                  #8

                  I should probably also mention I need the signals so that I can monitor the output as it progresses, just a dump at the end is not granular enough for my needs.

                  -=ben

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    QProcess, by it's nature, runs asynchronously by design. Why do you put that into a separate thread? Connecting to signals should be sufficient.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mirswith
                      wrote on last edited by
                      #10

                      Its the architecture of the tool I am developing. It's basically a system that queues a number of jobs, each job get's its own QThread to run in. A job then consists of one or more tasks defined with javascript, these scripts can then execute system tools using QProcess and I want these to be synchronous in their execution order.

                      -=ben

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        That sounds reasonable. But in that case, I would define my own signals and emit them after the process has finished and you have read stdout and/or stderr.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mirswith
                          wrote on last edited by
                          #12

                          If I wanted to implement my own waitForFinished() what would you recommend?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            The usual way of defining a new signal. That completely depends on your use case and needs. The outline is basically:

                            @
                            class MyProcessWorker : public ...
                            {
                            // constructors etc.

                            signals:
                            myProcessFinished(const QString &message);

                            protected:
                            void runMyProcess();
                            };

                            void MyProcessWorker::runMyProcess()
                            {
                            QProcess proc;
                            proc.start( args... );
                            proc.waitForFinished(-1);

                            QByteArray stdout = proc.readAllStandardOutput();
                            QByteArray stderr = proc.readAllStandardError();
                            
                            QString msg = whatever you make out of stdout and stderr;
                            
                            emit myProcessFinished(msg);
                            

                            }
                            @

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mirswith
                              wrote on last edited by
                              #14

                              Thanks, I understand signals/slots I was asking because I was running into an issue where I wanted a different thread to be able to cancel the process and if I sit on QProcesses waitForFinished() and then call kill() from a different thread then nasty things happen. :)

                              What I did in the end was simply set a bool that determines if I should kill the process and then after I call the process I do:

                              @
                              while(proc->waitForFinished(250))
                              {
                              if( _killRequested ) proc->kill();
                              }
                              @

                              This seems to be working well.

                              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