PDA

View Full Version : memory leaks detection in QT



kompotFX
27th January 2009, 21:23
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?

seim
27th January 2009, 22:05
have a look at valgrind

kompotFX
27th January 2009, 22:21
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?

wysota
27th January 2009, 22:40
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=windows+memory+leak+detection

kompotFX
27th January 2009, 23:47
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=windows+memory+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 ;-)

wysota
28th January 2009, 01:05
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.

spirit
28th January 2009, 14:09
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?


...
char* buf=new char[10];
...

you don't do this.

kompotFX
28th January 2009, 16:58
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.



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?

kompotFX
28th January 2009, 17:00
how will delete this array?

you don't do this.

hmmm, not sure I understood your question. Can u be more specific?

spirit
28th January 2009, 17:02
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


...
char* buf=new char[10];
...
delete[] buf;

wysota
28th January 2009, 18:41
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.

kompotFX
28th January 2009, 19:39
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.

kompotFX
28th January 2009, 19:41
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


...
char* buf=new char[10];
...
delete buf[];


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

spirit
28th January 2009, 19:42
sorry, did not read carefully. :)

seim
28th January 2009, 20:20
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..

wysota
28th January 2009, 22:29
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.