Unable to start a QProcess again
-
I am an experimenting with different APIs of QT. In the
private-section of my class I have a new process instance declared as follows:QProcess *myProcess;Then I initialize it in the constructor of said class as follows:
myProcess = new QProcess();I have a button in the GUI that executes the program
rsyncwhen pressed. Here is the most relevant section of the code that it executes:if (myProcess->state() != QProcess::NotRunning) { qDebug() << "Can't start a new process while the old one is still running."; return; } connect(myProcess, &QProcess::readyReadStandardOutput, [=](){ ui->consoleTextBrowser->append(myProcess->readAllStandardOutput()); }); connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus) { qDebug() << exitCode; qDebug() << exitStatus; myProcess->deleteLater(); }); myProcess->start(programString, rsyncArguments);The purpose of the
if-sentence in the beginning is to make sure that I am unable tostarta newrsyncprocess. Or at least that is my intention.Everything works fine the first time I run this. However, when I run this for the second time, the program crashes due to a segmentation fault. To my understanding the crash happens when the first
connectis called.What am I doing wrong here? Am I using these
connect-calls the wrong way? Should I somehow disconnect them before runningrsyncagain? -
Hi,
@Pothos said in Unable to start a QProcess again:
connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << exitCode;
qDebug() << exitStatus;
myProcess->deleteLater();
});You're nuking your myProcess object and likely not recreating it.
-
@SGaist
Oh, well damn it. I thought I was supposed to do that. Seems to fix the issue...However, I have an additional question related to this exact same topic:
How should I terminate an ongoingrsync-process started by this method, if I want to be able to run it again if I so choose? IsmyProcess->terminate()safe to call mid-execution? -
Terminate sends a SIGKILL which is likely not what you want.My memory failed me (good to re-read the documentation from time to time). So SIGTERM is used by QProcess.
You can use the standard kill method with SIGINT on the pid of the application that you can get from QProcess.
-
Terminate sends a SIGKILL which is likely not what you want.My memory failed me (good to re-read the documentation from time to time). So SIGTERM is used by QProcess.
You can use the standard kill method with SIGINT on the pid of the application that you can get from QProcess.
-
Memory leak should not happen however there will be no cleanup done. SIGKILL does not ask your process to stop in a nice and polite manner (that's the role of SIGINT), it executes it.
-
Memory leak should not happen however there will be no cleanup done. SIGKILL does not ask your process to stop in a nice and polite manner (that's the role of SIGINT), it executes it.
@SGaist
What do you think about sendingSIGTERMrather thanSIGINT?https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
Macro: int
SIGTERMThe
SIGTERMsignal is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate.The shell command
killgeneratesSIGTERMby default.Macro: int
SIGINTThe
SIGINT(“program interrupt”) signal is sent when the user types theINTRcharacter (normally C-c). See Special Characters, for information about terminal driver support for C-c.SITERM
According to ISO/IEC 9899:2011: a termination request sent to the program *SIGINT
According to ISO/IEC 9899:2011: receipt of an interactive attention signal *@Pothos
You can hope/assumersyncwill clean itself up. So long as you don't actually nuke it!You should test which signals work best with it.
-
@SGaist
What do you think about sendingSIGTERMrather thanSIGINT?https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
Macro: int
SIGTERMThe
SIGTERMsignal is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate.The shell command
killgeneratesSIGTERMby default.Macro: int
SIGINTThe
SIGINT(“program interrupt”) signal is sent when the user types theINTRcharacter (normally C-c). See Special Characters, for information about terminal driver support for C-c.SITERM
According to ISO/IEC 9899:2011: a termination request sent to the program *SIGINT
According to ISO/IEC 9899:2011: receipt of an interactive attention signal *@Pothos
You can hope/assumersyncwill clean itself up. So long as you don't actually nuke it!You should test which signals work best with it.
-
(Updated my answer above)
I would test and compare the two to see which one does what you want best.
-
@SGaist
@JonB
I see. So QProcess::terminate() is the way to go since it sends aSIGTERMto the process? -
@SGaist
@JonB
I see. So QProcess::terminate() is the way to go since it sends aSIGTERMto the process?