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. [SOLVED] Problem with cmd process
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with cmd process

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 5.2k 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.
  • jsulmJ jsulm

    @JonB And there are static methods in QProcess to execute a process without even creating a QProcess instance.

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #22

    @jsulm
    Ah!! (And you don't think those create an instance internally?). OK, so if I use static QProcess::startDetached() that really should not call ~QProcess, even on program exit?

    It's the whole point of startDetached() - it detaches the QProcess instance from the started process.

    Just because a process is detached that does not mean you cannot wait on or kill it, does it? It just means things like it's in its own session.

    But it should not matter as the process is detached and the destructor should NOT terminate it.

    OK, but I don't get that from the docs! Maybe we read them differently. I'm also having a deeper think about C++ static, too long now of having to do Python... :(

    Time for me to have a play....

    jsulmJ 1 Reply Last reply
    1
    • JonBJ JonB

      @jsulm
      Ah!! (And you don't think those create an instance internally?). OK, so if I use static QProcess::startDetached() that really should not call ~QProcess, even on program exit?

      It's the whole point of startDetached() - it detaches the QProcess instance from the started process.

      Just because a process is detached that does not mean you cannot wait on or kill it, does it? It just means things like it's in its own session.

      But it should not matter as the process is detached and the destructor should NOT terminate it.

      OK, but I don't get that from the docs! Maybe we read them differently. I'm also having a deeper think about C++ static, too long now of having to do Python... :(

      Time for me to have a play....

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

      @JonB said in Problem with cmd process:

      And you don't think those create an instance internally?

      I don't know. But it should not matter as the process is detached and the destructor should NOT terminate it.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      1
      • jsulmJ jsulm

        @JonB said in Problem with cmd process:

        And you don't think those create an instance internally?

        I don't know. But it should not matter as the process is detached and the destructor should NOT terminate it.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #24

        @jsulm
        Just to confirm your interpretation.

        From Python/PySide2, from a terminal if I run an interactive python3 and do

        >>> from PySide2.QtCore import QProcess
        >>> p = QProcess(); p.start("./script")
        

        and then exit the python session (python will auto-delete everything created), I get a message

        QProcess: Destroyed while process ("./script") is still running.
        

        But if I use

        >>> p = QProcess(); p.startDetached("./script")
        # or
        >>> QProcess.startDetached("./script")
        

        no message, and I continue to see ./script's output after the python session has exited.

        jsulmJ 1 Reply Last reply
        1
        • JonBJ JonB

          @jsulm
          Just to confirm your interpretation.

          From Python/PySide2, from a terminal if I run an interactive python3 and do

          >>> from PySide2.QtCore import QProcess
          >>> p = QProcess(); p.start("./script")
          

          and then exit the python session (python will auto-delete everything created), I get a message

          QProcess: Destroyed while process ("./script") is still running.
          

          But if I use

          >>> p = QProcess(); p.startDetached("./script")
          # or
          >>> QProcess.startDetached("./script")
          

          no message, and I continue to see ./script's output after the python session has exited.

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

          @JonB said in Problem with cmd process:

          no message, and I continue to see ./script's output after the python session has exited.

          This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          1
          • jsulmJ jsulm

            @JonB said in Problem with cmd process:

            no message, and I continue to see ./script's output after the python session has exited.

            This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #26

            @jsulm

            This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.

            Expected by you apparently, but not by me. If ~QProcess() is called, docs state

            Destructs the QProcess object, i.e., killing the process.

            Note that this function will not return until the process is terminated.

            If it said "but not when started via (non-static) QProcess::startDetached()" then I would be happy. Like I said, perhaps different doc interpretation between you & me.

            jsulmJ 1 Reply Last reply
            1
            • JonBJ JonB

              @jsulm

              This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.

              Expected by you apparently, but not by me. If ~QProcess() is called, docs state

              Destructs the QProcess object, i.e., killing the process.

              Note that this function will not return until the process is terminated.

              If it said "but not when started via (non-static) QProcess::startDetached()" then I would be happy. Like I said, perhaps different doc interpretation between you & me.

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

              @JonB Well, again:
              "If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetached

              And you even confirmed this behaviour by yourself :-)

              You can upload a patch fixing ~QProcess() documentation.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • jsulmJ jsulm

                @JonB Well, again:
                "If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetached

                And you even confirmed this behaviour by yourself :-)

                You can upload a patch fixing ~QProcess() documentation.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #28

                @jsulm
                I already wrote above: "calling process exits" does not tell you whether ~QProcess() is or is not called. In C++, if I glob_dangling = new QProcess(); exit(0); C++ cleanup does not call ~QProcess(), does it?

                But when ~QProcess() is called for whatever reason, https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess.cpp.html#_ZN8QProcessD1Ev has

                QProcess::~QProcess()
                {
                    Q_D(QProcess);
                    if (d->processState != NotRunning) {
                        qWarning().nospace()
                            << "QProcess: Destroyed while process (" << QDir::toNativeSeparators(program()) << ") is still running.";
                        kill();
                        waitForFinished();
                    }
                

                so presumably somewhere qProcess->startDetached() ends up causing d->processState = NotRunning.

                jsulmJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @jsulm
                  I already wrote above: "calling process exits" does not tell you whether ~QProcess() is or is not called. In C++, if I glob_dangling = new QProcess(); exit(0); C++ cleanup does not call ~QProcess(), does it?

                  But when ~QProcess() is called for whatever reason, https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess.cpp.html#_ZN8QProcessD1Ev has

                  QProcess::~QProcess()
                  {
                      Q_D(QProcess);
                      if (d->processState != NotRunning) {
                          qWarning().nospace()
                              << "QProcess: Destroyed while process (" << QDir::toNativeSeparators(program()) << ") is still running.";
                          kill();
                          waitForFinished();
                      }
                  

                  so presumably somewhere qProcess->startDetached() ends up causing d->processState = NotRunning.

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

                  @JonB If allocated on the stack destructor ALWAYS is called if app is closing in a clean way. If allocated on the heap you have to delete it. I'm sure Python has clean memory management and deletes what it allocates (so destrcutor is called).

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @JonB If allocated on the stack destructor ALWAYS is called if app is closing in a clean way. If allocated on the heap you have to delete it. I'm sure Python has clean memory management and deletes what it allocates (so destrcutor is called).

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #30

                    @jsulm said in Problem with cmd process:

                    If allocated on the heap you have to delete it. I'm sure Python has clean memory management and deletes what it allocates (so destrcutor is called).

                    Indeed exactly. So from C++ you have the choice to new somewhere and not delete, thereby avoiding destructor being called on exit. In Python you can't help destructor being called. Hence my requirement to understand ~QProcess.

                    As I said, I think we're just differing over what Qt docs might care to say in ~QProcess entry about what happens when pProcess->startDetached() was called. We'd better leave it at that :)

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      Cimmy
                      wrote on last edited by
                      #31

                      Thanks to all.
                      Sorry for my (many!) mistakes but i'm a beginner in c++ and qt programming.

                      JonBJ 1 Reply Last reply
                      0
                      • C Cimmy

                        Thanks to all.
                        Sorry for my (many!) mistakes but i'm a beginner in c++ and qt programming.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #32

                        @Cimmy
                        The "going out of scope" is a nasty problem which many people fall foul of, so don't worry :)

                        I should also apologise for getting this thread into a detailed discussion of an area I was interested. For you, follow @jsulm's advice about moving the QProcess variable out of the slot function and into a class member variable, and you should be good!

                        1 Reply Last reply
                        1

                        • Login

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