PDA

View Full Version : Qt example with memory leak



Squall
20th February 2011, 22:17
Hello.
I compiled a basic GUI example written with Qt. When I run with valgrind, it tells that there is memory leak.
It is stranger, because at each time I run the app, the number of malloc calls are different.
Qt seems nondeterministic and it does not free allocated memory.



#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QPushButton hello("Hello world!");
hello.resize(100, 30);

hello.show();
return app.exec();
}

SixDegrees
20th February 2011, 22:24
Qt does its own memory management for several classes that escapes detection by valgrind's checks for matched new/delete and malloc/free calls.

If you like, you can build a debug version of Qt and track these down yourself. When we did that, we wound up convincing ourselves that A) all the "leaks" we checked were spurious, and B) had we discovered any actual leaks, it would have been largely out of our control to fix them, since memory allocation takes place in code that could change with any future release.

ChrisW67
20th February 2011, 22:42
On my Linux machine with Qt 4.6.3 I see lost memory reports (possible and concrete) in libglib, libgobject, libfreetype, libX11 and few others. None of the leaks come directly from Qt.

Squall
20th February 2011, 23:22
Is there a way to detect actual memory leaks in a code that uses Qt?

ChrisW67
21st February 2011, 00:11
You already have it. Valgrind is a powerful tool but it is neither a no-brainer tool nor totally infallible. This might shed some light on the types of "leaks":
http://www.linuxprogrammingblog.com/using-valgrind-to-debug-memory-leaks

nightghost
21st February 2011, 10:07
IMHO valgrind is useless to debug a qt program without a suppression file. Buts very easy to create one. Write a very simple Qt Programm (only a few lines, but with QApplication, Dialog, etc; like the one in your example) and then use the Valgrind Suppression File Howto (http://wiki.wxwidgets.org/Valgrind_Suppression_File_Howto) to create one. You will get far less "qt errors" and can focus on your own code. Really nice is the Valgrind Eclipse Integration (http://www.eclipse.org/linuxtools/projectPages/valgrind/). That makes valgrind usage easier (at least for me ;))