How to delete a QProcess instance correctly?

Posted by Kopfschmerzen on Stack Overflow See other posts from Stack Overflow or by Kopfschmerzen
Published on 2010-04-26T09:00:19Z Indexed on 2010/04/26 9:03 UTC
Read the original article Hit count: 324

Filed under:
|

Hi everyone!

I have a class looking like this:

class FakeRunner : public QObject
{
    Q_OBJECT
private:
    QProcess* proc;
public:
    FakeRunner();
    int run()
    {
        if (proc)
            return -1;
        proc = new QProcess();
        QStringList args;
        QString programName = "fake.exe";

        connect(comp, SIGNAL(started()), this, SLOT(procStarted()));
        connect(comp, SIGNAL(error(QProcess::ProcessError)), this,
                SLOT(procError(QProcess::ProcessError)));
        connect(comp, SIGNAL(finished(int, QProcess::ExitStatus)), this,
                SLOT(procFinished(int, QProcess::ExitStatus)));

        proc->start(programName, args);

        return 0;
    };

private slots:
    void procStarted() {};
    void procFinished(int, QProcess::ExitStatus) {};
    void procError(QProcess::ProcessError);
}

Since "fake.exe" does not exist on my system, proc emits the error() signal. If I handle it like following, my program crashes:

void FakeRunner::procError(QProcess::ProcessError rc)
{
    delete proc;
    proc = 0;
}

It works well, though, if I don't delete the pointer. So, the question is how (and when) should I delete the pointer to QProcess? I believe I have to delete it to avoid a memory leak. FakeRunner::run() can be invoked many times, so the leak, if there is one, will grow.

Thanks!

© Stack Overflow or respective owner

Related posts about qt

Related posts about qt4