Results 1 to 16 of 16

Thread: memory leaks detection in QT

  1. #1
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default memory leaks detection in QT

    I'm trying to use QT creator 0.9.1 with built-in Qt version 4.4.3 (win32 & mingw).

    here is simple code which generates memory leak:
    #include <QtGui/QApplication>
    #include "mainwnd.h"

    int main(int argc, char *argv[])
    {
    char* buf=new char[10];

    QApplication a(argc, argv);
    MainWnd w;
    w.show();
    return a.exec();
    }
    trying to run application in debug mode. Closing application wont inform me about memory leaks. :-(
    Is it any QT methods/classes, which can inform me about memory leaks?
    What QT developers use for simple heap analysis?

  2. #2
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leaks detection in QT

    have a look at valgrind

  3. #3
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by seim View Post
    have a look at valgrind
    I guess valgrind is nothing todo with win32 platform.

    Is it any mem.detection built-in support inside QT classes? I see that QT is quite advanced library and I guess memory detection is like a basics "must have" for such a huge c++ library. I'm wrong?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: memory leaks detection in QT

    Why would Qt inform you about a leak in C code that has nothing to do with it? Qt is set of classes, not a new language/compiler/put-whatever-you-want-here. If you leak memory in your code, use a tool for detecting memory leaks in applications, not an application development framework.

    http://www.google.com/search?q=windo...leak+detection

  5. #5
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by wysota View Post
    Why would Qt inform you about a leak in C code that has nothing to do with it? Qt is set of classes, not a new language/compiler/put-whatever-you-want-here. If you leak memory in your code, use a tool for detecting memory leaks in applications, not an application development framework.

    http://www.google.com/search?q=windo...leak+detection
    thank you for very "informative" link and answer.
    Why shouldn't Qt inform you about a leak in C code?

    Check this microsoft stuff:

    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>

    int main(int argc, const char *argv[])
    {
    char* a = new char[10];
    _CrtDumpMemoryLeaks();
    return 0;
    }

    Detected memory leaks!
    Dumping objects ->
    ...normal block at 0x00383858, 10 bytes long.
    Data: < > CD CD CD CD CD CD CD CD CD CD
    Object dump complete.
    Is it so hard? I dont need more ;-)

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: memory leaks detection in QT

    Quote Originally Posted by kompotFX View Post
    Why shouldn't Qt inform you about a leak in C code?
    Because it has no access to this code.

    Check this microsoft stuff:
    The "microsoft stuff" calls the runtime to do the job and from what I see it is specific to the .NET runtime (I might be wrong, I just had a brief look at the docs), so no real C here. It will work only when using MSVC and only in the .NET versions, so it can hardly be called portable. Qt has no access to the kernel space nor to the heap code generated by the compiler. It works strictly on the C++ layer (with exception to assembly code occuring from time to time in low-level routines). Its work ends when the compiler's work begins.

    So if you use MSVC.NET, call _CrtDumpMemoryLeaks() when writing Qt code. Qt will not interfere but it has nothing to help you with - you can use any features the compiler allows you to, but don't ask every possible thing to be available in Qt - it is strictly a set of C++ libraries. People tend to forget (or ignore) that which is why my response was so harsh. Besides, I had a bad day so sorry if you felt offended, I didn't mean that.

    So to repeat again - Qt is not a programming language, it has no runtime, no bytecode, no intermediate execution, it doesn't extend C nor C++ language with any new features and it also doesn't limit the use of other C/C++ libraries and code and it doesn't influence alien code in any way. It is "just" a set of self-contained C++ libraries that provide you a portable API that uses and generates a standard C++ code. And it does its job (and its job only) quite well.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by kompotFX View Post
    I'm trying to use QT creator 0.9.1 with built-in Qt version 4.4.3 (win32 & mingw).

    here is simple code which generates memory leak:


    trying to run application in debug mode. Closing application wont inform me about memory leaks. :-(
    Is it any QT methods/classes, which can inform me about memory leaks?
    What QT developers use for simple heap analysis?
    how will delete this array?
    Qt Code:
    1. ...
    2. char* buf=new char[10];
    3. ...
    To copy to clipboard, switch view to plain text mode 
    you don't do this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by wysota View Post
    Because it has no access to this code.
    The "microsoft stuff" calls the runtime to do the job and from what I see it is specific to the .NET runtime (I might be wrong, I just had a brief look at the docs),
    I gues you are wrong. It works also with vs6 which has nothing todo with .net.

    Quote Originally Posted by wysota View Post
    So to repeat again - Qt is not a programming language, it has no runtime, no bytecode, no intermediate execution, it doesn't extend C nor C++ language with any new features and it also doesn't limit the use of other C/C++ libraries and code and it doesn't influence alien code in any way. It is "just" a set of self-contained C++ libraries that provide you a portable API that uses and generates a standard C++ code. And it does its job (and its job only) quite well.
    Partly agree. But I think that "QT" is not only about to make things "portable", but also for RAD and according headlines on qt webpage its not just self-contained C++ libraries, but also set of development tools. It could be handy to have this basic one in the tools list as well ;-)

    I have not so good understanding of heap analisys things (othervice wont post this topic here), but I think basic idea is to overload new, delete, malloc, free etc. with some heap counting code. This approach can be easily(?) implemented in qt library without any language extention. Whatever... if not such tool exist in QT, will search for external one. Not a big problem. Thanks anyway...

    P.S. What personally You are using in qt development process to detect memory leaks?

  9. #9
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by spirit View Post
    how will delete this array?

    you don't do this.
    hmmm, not sure I understood your question. Can u be more specific?

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: memory leaks detection in QT

    you create an array on the heap, but don't release memory after using this cause a memory leak about which you are talking, so you need to do the next
    Qt Code:
    1. ...
    2. char* buf=new char[10];
    3. ...
    4. delete[] buf;
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 28th January 2009 at 20:10.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: memory leaks detection in QT

    Quote Originally Posted by kompotFX View Post
    Partly agree. But I think that "QT" is not only about to make things "portable", but also for RAD and according headlines on qt webpage its not just self-contained C++ libraries, but also set of development tools. It could be handy to have this basic one in the tools list as well ;-)
    You don't want a tool, you want a function and that's different. There is no point in creating a tool that does exactly the same as bunch of already existing tools. Qt Software uses Valgrind to detect memory leaks and it works great, why mimic it if it is already there?

    I have not so good understanding of heap analisys things (othervice wont post this topic here), but I think basic idea is to overload new, delete, malloc, free etc. with some heap counting code.
    Yes, for every possible overload. Existing and non-existing in Qt, so in practice this is not possible inside a regular library.

    This approach can be easily(?) implemented in qt library without any language extention.
    Unfortunately not, because if you want to do it properly by substituting malloc() and free(), you have to prevent the original implementation from butting in. You can do that only by preventing the dynamic linker from loading the definition from the C runtime. On Linux this can be done by using LD_PRELOAD. But there is no way (at least I don't know one) to do it globally (meaning for all the code in tha application) by calling some C/C++ statement or passing a compiler/linker option to the compiler - you can do that for your own code, but not for the code used by 3rd party libraries you link against. That's why I said you could do leak detection in Qt-based code, but not in plain C/C++/3rd party code.

    Whatever... if not such tool exist in QT, will search for external one.
    If you want to substitute malloc and free, you can use mpatrol or a similar solution.

    P.S. What personally You are using in qt development process to detect memory leaks?
    I don't do memory leaks - my mind seems to be sufficient to control myself. But if I really need to check for leaks, I use Valgrind, it's the best tool for the job and it does more than just detect leaks, it reports all sorts of memory related problems.

  12. #12
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by wysota View Post
    I don't do memory leaks - my mind seems to be sufficient to control myself. But if I really need to check for leaks, I use Valgrind, it's the best tool for the job and it does more than just detect leaks, it reports all sorts of memory related problems.
    unfortunately I cannot use valgind bsc I'm using Windows XP and mingw :-(

    I can control my self as well and hopefully not doing memory leaks on purpose. But if we talking about 100 000 lines of code, several developers and not well documented 3rd party libraries where is not clear who takes ownership of passed objects, than this kind of tool is highly needed.

  13. #13
    Join Date
    Jan 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: memory leaks detection in QT

    Quote Originally Posted by spirit View Post
    you create an array on the heap, but don't release memory after using this cause a memory leak about which you are talking, so you need to do the next
    Qt Code:
    1. ...
    2. char* buf=new char[10];
    3. ...
    4. delete buf[];
    To copy to clipboard, switch view to plain text mode 
    yes, I know this :-) I'm talking about tool that can warn me about this kind of mistakes.

    P.S. btw: "delete[] buf;" is works better
    Last edited by kompotFX; 28th January 2009 at 19:54.

  14. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: memory leaks detection in QT

    sorry, did not read carefully.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. #15
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leaks detection in QT

    Quote Originally Posted by kompotFX View Post
    unfortunately I cannot use valgind bsc I'm using Windows XP and mingw :-(

    I can control my self as well and hopefully not doing memory leaks on purpose. But if we talking about 100 000 lines of code, several developers and not well documented 3rd party libraries where is not clear who takes ownership of passed objects, than this kind of tool is highly needed.
    1. Do not use M$...
    2. If you can not, look for an existing memory leak tool...
    3. If you do not want to.. write a new tool for yourself. It shouldn't be a big deal for you to write such a thing for debuging purposes - with support of ABI of your "specific" compiler under MinGW you should be able to provide may be also some other things..
    Last edited by seim; 28th January 2009 at 20:48.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: memory leaks detection in QT

    Quote Originally Posted by kompotFX View Post
    unfortunately I cannot use valgind bsc I'm using Windows XP and mingw :-(
    Sure you can, you can switch to Linux and develop there. Qt apps are portable, remember?

    More seriously, there are similar tools for Windows (maybe not as powerful), so nothing is lost.

    Try one of these: http://www.aptest.com/resources.html#app-source

    I can control my self as well and hopefully not doing memory leaks on purpose. But if we talking about 100 000 lines of code, several developers and not well documented 3rd party libraries where is not clear who takes ownership of passed objects, than this kind of tool is highly needed.
    Well, first of all I'm talking about my own code. Second of all documenting your own code really helps. Third of all there is not much you can do about leaks in 3rd party libraries. And fourth of all avoiding bare pointers really helps.

Similar Threads

  1. Memory Leak Detection Tool
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2008, 16:44
  2. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 20:28
  3. Memory leak Detection using Valgrind
    By joseph in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 08:34
  4. Memory leaks when read/write to DOM
    By SailinShoes in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2008, 12:51
  5. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 09:00

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.