QProcess and shell : Destroyed while process is still running

帅比萌擦擦* 提交于 2019-12-01 03:56:32

问题


I want to launch a shell script with Qt.

QProcess process;
process.start(commandLine, QStringList() << confFile);
process.waitForFinished();

if(process.exitCode()!=0)
{
    qDebug () << " Error " << process.exitCode() << process.readAllStrandardError();
}
else
{
    qDebug () << " Ok " << process.readAllStrandardOutput() << process.readAllStrandardError();
}

The result is :

Ok : Result.... " "" QProcess : Destroyed while process is still running.

This message does not appear every time.

What is the problem?


回答1:


process.waitForFinished(); is hitting the default 30 seconds timeout. Use process.waitForFinished(-1); instead. This will make sure you wait for however long it takes for the process to finish, without any timeout.




回答2:


Note you create QProcess into the local scope. This means that the object will be deleted when you exit the scope. In the destructor QProcess process terminates. The message "Destroyed" while "the process is still running" when the process terminates in the destructor.

For solving this problem, you should call QProcess destructor when process is already terminated.

If will be QProcess::waitForFinished(-1) into your example, it will occur, but this will block you application.



来源:https://stackoverflow.com/questions/14504201/qprocess-and-shell-destroyed-while-process-is-still-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!