QProcess::execute works, QProcess::startDetached doesn't
-
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...
-
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...
@ModelTech
That's because yourQProcess Process
looks like a local variable. It goes out of scope, gets destroyed, and terminates the process when it goes out of scope. OTOHQProcess::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 forQProcess::startDetached()
, as that keeps running even after your application exits, so theQProcess
gets destroyed unconditionally.... Anyway, try with theQProcess
as a class-lifetime variable ornew
ed and see if that fixes, else report back? -
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...
-
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...
@ModelTech
As @JonB already explained,startDetached()
doesn't work when theQProcess
is 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 withnew
and make sure it doesn't leak. -
@ModelTech
As @JonB already explained,startDetached()
doesn't work when theQProcess
is 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 withnew
and make sure it doesn't leak.@Axel-Spoerl
Did you actually try this withstartDetached()
? I cannot as I am "abroad" :) As per my last paragraph, I thought about it and am unconvinced. While this is certainly true forQProcess::start()
, forstartDetached()
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 theQProcess
object does not terminate the subprocess in this case? -
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...
@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.