Is QProcess::finished emitted if the child process crashes?

半腔热情 提交于 2019-12-08 08:03:51

问题


The documentation says that the error() signal will be emitted if the child process crashes, but would finished() be emitted as well or is it only emitted when it exits successfully?


回答1:


Yes. And it returns you status, as docs state:

void QProcess::finished ( int exitCode, QProcess::ExitStatus exitStatus ) [signal]

QProcess::NormalExit    0   The process exited normally.
QProcess::CrashExit 1   The process crashed.



回答2:


You can find out by testing it. Write a small program that does a NULL pointer dereference (this will guarantee a crash):

struct Foo {
    int a;
};

int main()
{
    Foo* foo = 0;
    int d = foo->a;
}

Make sure you build without optimization so that the dereference doesn't get optimized out. Then run it in a QProcess and check whether finished() is being emitted.




回答3:


The answer to the question is, as other have noted, "Yes."

And that feels like a problem to me because without a reference to the object that emitted the signals you have to do something like:

void on_finished( int exitCode, QProcess::ExitStatus exitStatus )
{
    if ( existStatus == QProcess::CrashExit )
    {
        // We'll handle in on_errorOccured()
        return;
    }

    // ...
}


void on_errorOccured( QProcess::ProcessError error )
{
    // ...
}

As an alternate to accepting I wrote a thin wrapper class that connects (only!) to QProcess::stateChanged(QProcess::ProcessState newState) and works out what happened using newState and calling the QProcess object:

void ProcessWrapper::on_stateChanged(QProcess::ProcessState newState)
{
    switch (newState)
    {
    case QProcess::Starting:
        // No action needed
        break;
    case QProcess::Running:
        emit( started( this ) );
        break;
    case QProcess::NotRunning:
        if ( m_process.exitStatus() != QProcess::NormalExit )
            emit( crashed( this, m_process.error() ) );
        else
            emit( finished( this, m_process.exitCode() ) );
        break;
    }
}

The signals the wrapper emits have two properties:

  • Only one signal is emitted when a process ends, and which one tells you how it ended (normally or abnormally).
  • Each signal includes both a code for how things did and a pointer to the object so that the user can inquire further.


来源:https://stackoverflow.com/questions/12991509/is-qprocessfinished-emitted-if-the-child-process-crashes

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