I am researching this but I don't see a conclusive answer. Does a Qt widget application clean up the memory when it exits? Does it make any difference deriving from QObject? If there is garbage collection than why is there QSharedPointer
class? I am asking from the following context of my code.
void MainWindow::sync()
{
QString destPathUnixStyle = makePathUnix( _RootPath );
QString rsync_cmd = QString("rsync/rsync -a root@%1:/data/ '%2'").arg( _ip ).arg( destPathUnixStyle );
QProcess *syncProcess = new QProcess(this);
syncProcess->start( rsync_cmd );
qDebug() << "Sync started..";
connect(syncProcess, SIGNAL(finished(int)), this, SLOT(syncFinished()) );
_syncInProgress = true;
}
Now will my syncProcess
be cleaned up when application exits? What if user calls this function a thousand times without exiting, will it be creating memory leak?
Update
Given that my function above is called frequently many many times, is it better to declare the QProcess
a member variable or just used QSharedPointer
to improve the code above?
new QProcess(this);
, you made the newQProcess
object owned bythis
instance ofMainWindow
. When aQObject
is destroyed, it in turn destroys all objects it owns. – HooghlyQSharedPointer
or makeQProcess
a member variable? – Freedwomanfinished
handler, I suppose.QObject::deleteLater
is convenient for this (it avoids deleting an object while in that object's signal). – Hooghly