Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Qt applications have automatic garbage collection?

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?

like image 790
zar Avatar asked Aug 27 '15 14:08

zar


People also ask

Does Qt have garbage collection?

Qt doesn't have garbage collection. It has a concept of ownership. With new QProcess(this); , you made the new QProcess object owned by this instance of MainWindow . When a QObject is destroyed, it in turn destroys all objects it owns.

What is garbage collection software?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

Which operator is used for garbage collection?

Automatic Garbage Collection in Java is the most important feature of the language and is a part of memory management in Java. Though Garbage Collection is performed by JVM and is out of the programmer's reach, we can always request the Garbage Collector to run using the gc () method of the System and Runtime class.

When should you not use garbage collection?

One fundamental problem with garbage collection, though, is that it is difficult to estimate and manage the actual size of the working set in memory, because garbage collector can free your memory only delayedly. So, yes, when memory is restricted, garbage collection might not be a good choice.


2 Answers

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.

like image 157
Chase Henslee Avatar answered Sep 24 '22 08:09

Chase Henslee


Qt handles a "ownership" tree structure. A QObject may have a set of children and if it gets deleted then it will delete all of its children.

In your code the syncProcess will get deleted when the this that you passed gets deleted or when it explicitly gets deleted.

You can let it delete itself after it sends the signal by connecting the finished signal to its own deleteLater slot.:

connect(syncProcess, SIGNAL(finished(int)), syncProcess, SLOT(deleteLater()) );
like image 44
ratchet freak Avatar answered Sep 25 '22 08:09

ratchet freak