2

I am writing a Qt app to compile and run C++ files. I don't know how can I check for runtime errors such as segmentation fault after the process has been started. I tried using error() signal for reporting runtime error, but it doesn't tell anything about segmentation fault.

//connected to error() signal
void Tester::onError(QProcess::ProcessError e)
{
    switch(e) {
    case QProcess::Crashed:
    case QProcess::WriteError:
    case QProcess::ReadError:
    case QProcess::UnknownError:
        QMessageBox::information(NULL, "Run status", "Runtime error",
                                 QMessageBox::Ok | QMessageBox::Default);
        break;
    default:
        break;
    }
}

This is the file I am testing it with-

#include <iostream>

int main() {
    int *p = NULL;
    *p = 1;  //segmentation fault
    std::cout << *p;
    return 0;
}

When I test the file from the Qt app, the new process runs for about 15-20 seconds and then finished() signal is emitted, with a weird exit code and a message box saying "runtime error". How can I detect that there had been a segmentation fault in the new QProcess so that I can display it to the user?
I am working on Windows 7.

Shubham
  • 935
  • 1
  • 7
  • 25

1 Answers1

3

Error reporting with QProcess is a bit confusing - one needs to connect to both error() and finished() to cover all error cases.

If you connect to finished(int exitCode, QProcess::ExitStatus status), a segfault should result in a status of QProcess::CrashExit.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • It kind of does the job, but `QProcess::CrashExit` can be because of other reasons too, so I cannot use it to pin point segfault. – Shubham Feb 14 '14 at 12:22
  • 2
    It's detectable at least on Unix. See http://linux.die.net/man/3/wait and WTERMSIG(). Qt uses WIFEXITED() for CrashExit, so it's indeed not sufficient to pinpoint SIGSEGV. But that's a limitation of the QProcess API, not of the OS. – Frank Osterfeld Feb 15 '14 at 08:10