Is this an efficient use of QProcess?
-
What I have works, but I'm just curious if it's the best way to go about this. I'm re-using a process to execute various external .exe files and pass them arguments.
process = new QProcess(this); process->execute(file, args); process->waitForFinish(); process->close(); process = new QProcess(this); process->execute(file2, args2); process->waitForFinish(); process->close(); etc.....
Like I said this is working, but I'm just wondering if I'm being efficient or not. Maybe there is a better way to do this? I do need these processes to happen in sequence as well, so the forced wait and close and restart ensures the sequence is always correct.
-
if your code is in a thread, this is Ok. otherwise it would be better to connect the
finished
signal to a slot and start the next process from the to avoid blocking your event loop. -
Hi,
You are leaking Process objects. While you are giving them a parent, they won't be deleted until the parent object is so currently you might just be filling your memory.
Why not just create one QProcess on the stack and then execute each command in sequence with that object ? You could even have a vector of
QPair<QString, QStringList>
that would be your commands with their respective parameters and use a for loop to execute them all. -
@graniteDev yeah, should work.
just leave out the subsequent
new
lines.@Lifetime-Qt-Champion well spotted , didn't see the leak...
-
I didn't realise... You are using
execute
which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing. -
@SGaist said in Is this an efficient use of QProcess?:
I didn't realise... You are using
execute
which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing.In which case, how come his
process->waitForFinish();
even returns/doesn't error, because as you say the instance has never beenstart()
ed? -
waitForFinished
has a timeout with a default value but in this case, since the process is not even running it will return early. -
I wonder if the confusion is that I left out the beginning code, the first process starts out:
QProcess *process = new QProcess(this); process->execute(file1,args1); process->waitforFinish(); process->close(); process = new QProcess(this); process->execute(file2,args2); process->waitforFinish(); process->close(); process = new QProcess(this); process->execute(file3,args3); process->waitforFinish(); process->close();
I get no compile errors, and it's working. Per the Qt documentation close() deletes the object, so I don't believe I'm creating a memory leak with what I'm doing.
However, if I can eliminate creating a new object and then deleting it every time, I'm for improving the code.
-
@graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.
@JonB
waitForFinished
will return false because of that. -
QProcess::execute(file1, args1);
-
Did you read the documentation of the function ?
execute
runs the command and waits for it to finish. The returned value indicates what happened. -
What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by
execute
.