Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Dynamic QProcess



  • Hi,

    I created a QProcess pointer in mainwindow.h.

    QProcess *process;
    

    On the GUI, I have one button that starts the process when pressed.

    process = new QProcess();
    process->start(path);
    

    If I create new objects by pressing a button again, will it be safe or will there be a memory leak?



  • @Creatorczyk If you create something with "new" and there is never a corresponding delete, then there will absolutely be a memory leak and nothing will ever destruct the QProcess object and free up the memory it used.



  • @Creatorczyk
    As @wrosecrans has said, it is your responsibility to delete the QProcess when you are done with it. For a simple example, if you are not interested in anything further from the QProcess after it has finished, you might do something like

    connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            process, &QObject::deleteLater);
    

    In practice you should also deal with QProcess::errorOccurred() similarly. The sample just illustrates the kind of thing you should do.


Log in to reply