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. QProcess::execute works, QProcess::startDetached doesn't
Forum Updated to NodeBB v4.3 + New Features

QProcess::execute works, QProcess::startDetached doesn't

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.3k 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    I need some help. I have the following code that I am trying to get working on Windows with Qt5.12, where Temp is a QTemporaryFile (with spaces in its name):

    QProcess Process;
    Process.setProgram(Executor);
    QStringList Arguments;
    Arguments << Temp.fileName();
    Process.setArguments(Arguments);
    Process.startDetached();
    

    Somehow this is not starting any process. However, if I use

    QProcess::execute(Executor, Arguments);
    

    for the very same values of Executor and Arguments, the process does get executed. Therefore, I assume that the values of these are also ok when using QProcess::startDetachted.

    Any ideas what I am doing wrong?

    BTW: QProcess::start also doesn't work...

    JonBJ 1 Reply Last reply
    0
    • ModelTechM ModelTech

      I need some help. I have the following code that I am trying to get working on Windows with Qt5.12, where Temp is a QTemporaryFile (with spaces in its name):

      QProcess Process;
      Process.setProgram(Executor);
      QStringList Arguments;
      Arguments << Temp.fileName();
      Process.setArguments(Arguments);
      Process.startDetached();
      

      Somehow this is not starting any process. However, if I use

      QProcess::execute(Executor, Arguments);
      

      for the very same values of Executor and Arguments, the process does get executed. Therefore, I assume that the values of these are also ok when using QProcess::startDetachted.

      Any ideas what I am doing wrong?

      BTW: QProcess::start also doesn't work...

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

      @ModelTech
      That's because your QProcess Process looks like a local variable. It goes out of scope, gets destroyed, and terminates the process when it goes out of scope. OTOH QProcess::execute() blocks till finished, so you don't have the scope/lifetime issue.

      [Thinking aloud now.] The foregoing is at least true for QProcess::start(). It's less clear to me (I can't test) what happens for QProcess::startDetached(), as that keeps running even after your application exits, so the QProcess gets destroyed unconditionally.... Anyway, try with the QProcess as a class-lifetime variable or newed and see if that fixes, else report back?

      1 Reply Last reply
      3
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        Well, the problem turns out to be at a very different place... QProcess::startDetached does work as used above but the called process crashes almost immediately.

        When I do a normal run, the QTemporaryFile is not created at all, while it does get created when I run stepwise in debug mode. But even then, there is a problem with the executed process in that Qt Creator seems to set PYTHONHOME which it shouldn't as that breaks the called process for using a different Python installation...

        Axel SpoerlA C 2 Replies Last reply
        0
        • ModelTechM ModelTech

          Well, the problem turns out to be at a very different place... QProcess::startDetached does work as used above but the called process crashes almost immediately.

          When I do a normal run, the QTemporaryFile is not created at all, while it does get created when I run stepwise in debug mode. But even then, there is a problem with the executed process in that Qt Creator seems to set PYTHONHOME which it shouldn't as that breaks the called process for using a different Python installation...

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          @ModelTech
          As @JonB already explained, startDetached()doesn't work when the QProcessis allocated on the heap. If you step in with the debugger, it may do some wonders. But if you start it within the application, the execution will be terminated as soon as the object goes out of scope. You have to allocated it with newand make sure it doesn't leak.

          Software Engineer
          The Qt Company, Oslo

          JonBJ 1 Reply Last reply
          0
          • Axel SpoerlA Axel Spoerl

            @ModelTech
            As @JonB already explained, startDetached()doesn't work when the QProcessis allocated on the heap. If you step in with the debugger, it may do some wonders. But if you start it within the application, the execution will be terminated as soon as the object goes out of scope. You have to allocated it with newand make sure it doesn't leak.

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

            @Axel-Spoerl
            Did you actually try this with startDetached()? I cannot as I am "abroad" :) As per my last paragraph, I thought about it and am unconvinced. While this is certainly true for QProcess::start(), for startDetached() the point is that you call it and then you can exit your program and the subprocess will keep running. So I'm thinking it must do something so that destruction of the QProcess object does not terminate the subprocess in this case?

            1 Reply Last reply
            0
            • ModelTechM ModelTech

              Well, the problem turns out to be at a very different place... QProcess::startDetached does work as used above but the called process crashes almost immediately.

              When I do a normal run, the QTemporaryFile is not created at all, while it does get created when I run stepwise in debug mode. But even then, there is a problem with the executed process in that Qt Creator seems to set PYTHONHOME which it shouldn't as that breaks the called process for using a different Python installation...

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #6

              @ModelTech said in QProcess::execute works, QProcess::startDetached doesn't:

              When I do a normal run, the QTemporaryFile is not created at all,

              It does get created. I assume you have a stack-based QTemporaryFile object so that, "the file will subsequently be removed upon destruction of the QTemporaryFile object." So as you leave that scope the file vanishes. Whether your child process ever sees the file will simply be a race. I expect that the process starts faster than the code finishes the scope rarely, if ever.

              Look at QTemporaryFile::setAutoRemove() to alter that behaviour. Your child process needs to clean up the temporary file itself.

              while it does get created when I run stepwise in debug mode.

              Yes, while you are paused inside the scope that contains the QTemporaryFile object the associated temporary file will exist.

              But even then, there is a problem with the executed process in that Qt Creator seems to set PYTHONHOME which it shouldn't as that breaks the called process for using a different Python installation...

              Qt Creator, by default, just passes on the environment it inherited. This is all configurable in the project settings.
              f388e70e-d7b4-41bb-add5-09d57827fdef-image.png
              35754f83-fa11-47ba-973c-41f566d70fc2-image.png

              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