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 theQProcess
when you are done with it. For a simple example, if you are not interested in anything further from theQProcess
after it has finished, you might do something likeconnect(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.