Question

I'm trying to compile a Qt 4 Application which contains the call of QProcess.

It's no problem to compile it as Release-Version, but I always get a SegmentionFault-Error when i execute the Debug-Version.

My Qt version is 4.8.2 and i tried it on Fedora and Debian OS.

Does anyone of you ever had similar problems? I can't figure it out!

#include <QApplication>
#include "mainwindow.h"
#include <QProcess>

class MyApplication : public QApplication{
public:
    QProcess * proc;
    MyApplication(int argc, char** argv) : QApplication(argc, argv){
        proc = new QProcess();
    }
};

int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

Have a nice day, Markus

Was it helpful?

Solution

The crash is caused by this line:

MyApplication(int argc, char **argv);

You should modify it to:

MyApplication(int &argc, char **argv);

The reason is that, without reference, there is a temporary copy included which basically gets destroyed, while the address of it is still stored. The crash happens at the access afterwards.

See the warning in the documentation of the QApplication constructor:

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

That being said, it is probably not worth subclassing QApplication in your case. Even if you do, make sure you do not leak the memory by not deleting the heap object created for handling the background process with QProcess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top