Does Qt applications have automatic garbage collection?

zar picture zar · Aug 27, 2015 · Viewed 8.5k times · Source

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 QSharedPointerto improve the code above?

Answer

Chase Henslee picture Chase Henslee · Aug 27, 2015

QT does not use garbage collection, instead it uses reference counting (in the case of QSharedPointers) and object ownership (in the case of your example).

In your case, The QProcesses will be destroyed when your MainWindow class is destroyed.

edit: https://stackoverflow.com/a/19332239/841330 RobbieE's answer is really good.