PDA

View Full Version : QT Memory Leaks



lukabratzi
28th August 2010, 20:26
Hey everyone,

I just finished my first QT application for displaying a directed graph. The only issue is that it seems to have a massive amount of memory leaks (~2400), even though I delete all of the objects that I create. I would be able to understand a smaller amount of leaks, but I can't fathom this many. Is there any reason that QT would cause such a huge amount of leaks?

I'm fairly certain that its QT because I tested the leaks with multiple input files and number of nodes. One input file had about 20 nodes, while another had 4 or 5. For some reason, both files generated the same number of leaks. So that makes me think that the leak may be in QT due to the lack of scaling with the number of objects created.

The other thought that I'm having is that this may be due to some incompatibility between QT and VLD. Some googling tells me this may be the case, does anyone have any experience with this or suggestions as to how to fix it?

Thank you.

tbscope
28th August 2010, 20:44
The only issue is that it seems to have a massive amount of memory leaks (~2400), even though I delete all of the objects that I create.

Since measuring isn't only about the results but more importantly about how the measurement is done, can you give a description of how the measurement is done and how you get the number 2400?

lukabratzi
28th August 2010, 21:09
Sure, I'm not really sure what you mean by your question but I'll do my best to explain it.

I just use #include "vld.h" at the top of my main function and run the program in debug mode. When it finishes execution vld outputs the status of my memory leaks. Its nothing that I did, just a library that I include in the source code.

tbscope
28th August 2010, 21:15
Yes, that's in the direction of my question.

I don't know vld. Can you give a url please?

lukabratzi
28th August 2010, 21:21
Sure, here it is: VLD link (http://www.codeproject.com/KB/applications/visualleakdetector.aspx?msg=1073439)

There's a post at the bottom of the page called Qt that may be relevant. Its where I'm getting my assumption that the issue lies with QT/vld integrating

squidge
28th August 2010, 21:25
VLD can support stack tracing of each leak, so you can see where the leaks are occuring from.

tbscope
28th August 2010, 21:28
The problem you can have with in place (or in the code of your program itself) memory checking tools is the Heisenberg principle. When you are part of the system and you try to measure it, you will change the system.

From onderstanding the posts on that site, the memory checking tool can be finished before the qt library is finished, resulting in false positives.

On linux there's a tool called valgrind witch is not part of your program so it can measure more accurate

lukabratzi
28th August 2010, 21:31
Are there any windows alternatives to valgrind? My code was written on a windows pc and I don't currently have access to a linux pc.

Also, the output from vld looks like this:
0xFEEEFEEE (File and line number not available): (Function name unavailable)

So I can't really trace the leaks and see where they're originating because vld can't tell me.

tbscope
28th August 2010, 21:32
If you want the stack traces to work, you also need to compile the Qt library in debug mode and link your program to the debug version of the Qt library.

lukabratzi
28th August 2010, 21:35
Oh okay, that explains a lot about the error messages. I'm currently compiling with visual studio 2008 using the qt plugin in debug mode. How can I compile/link to the qt library in debug mode?

tbscope
28th August 2010, 21:39
There's a configure option. Before you build the Qt library, you need to configure it. Use -debug (or something like that) instead of -release.
Then, when it is installed, use the qmake of the debug version of the qt library. This qmake version will link to all the qt debug libraries.

Thus:
configure qt with -debug
build it
install it to a certain directory (example: c:\qtdebug)
use qmake from this version (example: c:\qtdebug\bin\qmake c:\myproject\myproject.pro)

tbscope
28th August 2010, 21:43
But:
Even then the stack traces won't be useful. You get a list of thousands of lines with no real information because all those objects are actually deleted after the report from your tool is done.

lukabratzi
28th August 2010, 22:09
So what other options do I have to fix this? By the way, thanks everyone for all the help.

squidge
29th August 2010, 00:06
But:
Even then the stack traces won't be useful. You get a list of thousands of lines with no real information because all those objects are actually deleted after the report from your tool is done.

The idea behind VLD is that it starts at main, and does the output when main returns. Therefore, Qt should be fully finished by that time. Whatever is left is assumed to be leaks. Of course, it's not perfect, but when you know the limitations, it can be quite useful.

As for the stack, the trace is captured at memory allocation time (not at leak detection time), so it should still be able to point you in the right direction.

tbscope
29th August 2010, 06:15
Suggestion for improvement using VLD:

1. Make sure you include the VLD header as the absolute very first header of your complete program.
2. Make sure that if VLD is a library, it is loaded as the very first library of your complete program.

While I don't think Qt is free of memory leaks, I don't think it contains 2400+ leaks. That's madness.

SixDegrees
29th August 2010, 13:08
I'm fairly sure that Qt does a lot of it's own memory management, bypassing malloc and new. This is certainly true for QStrings, which do a lot of referencing to avoid unnecessary data copies. Unfortunately, this sort of optimization hoses most leak checkers; a lot of them simply match calls to malloc/free or new/delete and note when there's an imbalance.

Since the number of leaks appears to be constant, rather than increasing with program load, I'd guess this is the case. See if your program actually consumes an increasing amount of memory over time and with increasing program load. If not, these reports can probably be safely ignored. I don't know what sort of tools are available for this under Windows; the Task Manager is notorious for giving bogus memory usage reports.

Note, too, that you may have to build Qt in debug mode in order to get the most useful information out of these tools; in release mode, they'll tend to be accurate, but not precise.

hitesh_
28th February 2012, 06:25
can any one plz tell me that how to use VLD whit Qt Creator 2.2.1
i have download "vld-10.zip"
in which i got following files
1) vld.h
2) vldapi.h
3) dbghelp.dll
4) vld.lib
5) vldmtdll.lib
6) vldmt.lib

plz help me that i acn use it with Qt Creator 2.2.1

amleto
28th February 2012, 20:01
I'm fairly sure that Qt does a lot of it's own memory management, bypassing malloc and new.

you can't allocate to the heap without malloc/new.

also, if you add -widgetcount (iirc) to command line options then Qt gives its own 'memory report' at the end.
http://doc.trolltech.com/4.6/qapplication.html#QApplication