Results 1 to 18 of 18

Thread: QT Memory Leaks

  1. #1

    Default QT Memory Leaks

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    Quote Originally Posted by lukabratzi View Post
    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?

  3. #3

    Default Re: QT Memory Leaks

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    Yes, that's in the direction of my question.

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

  5. #5

    Default Re: QT Memory Leaks

    Sure, here it is: VLD link

    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

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Memory Leaks

    VLD can support stack tracing of each leak, so you can see where the leaks are occuring from.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    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

  8. #8

    Default Re: QT Memory Leaks

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    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.

  10. #10

    Default Re: QT Memory Leaks

    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?

  11. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    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)

  12. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    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.

  13. #13

    Default Re: QT Memory Leaks

    So what other options do I have to fix this? By the way, thanks everyone for all the help.

  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Memory Leaks

    Quote Originally Posted by tbscope View Post
    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Memory Leaks

    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.

  16. #16
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QT Memory Leaks

    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.

  17. #17
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QT Memory Leaks

    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
    Last edited by hitesh_; 28th February 2012 at 06:47.

  18. #18
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Memory Leaks

    Quote Originally Posted by SixDegrees View Post
    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/qapplic...l#QApplication
    Last edited by amleto; 28th February 2012 at 20:07.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. memory leaks detection in QT
    By kompotFX in forum Qt Programming
    Replies: 15
    Last Post: 28th January 2009, 21:29
  2. Memory Leaks
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2008, 16:26
  3. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 19:28
  4. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00
  5. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.