15

I have been assigned to check memory leak for an API by my boss. The Application is created in C & C++. So there is a possibility that memory is allocated using malloc & new. I want to check the memory leak in Visual Studio 2010 in debugger mode in 64 bit Windows 7. The problem with task manager is that it is not showing stable readings (memory increasing & decreasing by small amounts). Also the difference is small before & after the API is run. So i cannot defitely say that x amount of memory is leaking per cycle.

I have searched on the internet & found that linux has a great tool for this. However I want a reliable tool for my requirements (Windows 7). I have come across these:

http://winleak.sourceforge.net/

http://sourceforge.net/projects/duma/?source=recommended

As mentioned over here:

check Memory leaks in windows

the tool

http://technet.microsoft.com/en-us/library/bb457063.aspx

is not useful for my requirements. It would be very helpful of you guys if you could please suggest a good tool, as the customer who is requesting this is very important for our company. Thank You!

Community
  • 1
  • 1
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • 8
    unfortunately windows and free do not go along in the same sentence – BЈовић Jan 09 '13 at 13:16
  • 4
    and whats wrong with visual leak detector: http://vld.codeplex.com/ ? – Kamil Klimek Jan 09 '13 at 13:16
  • @KamilKlimek, Thanks man!!! I didnt get that by google. BTW could you also suggest an application which can be used on any exe()i.e. not just for Visual Studio projects. – Cool_Coder Jan 09 '13 at 13:20
  • Is build instrumentation an option? – Ivaylo Strandjev Jan 09 '13 at 13:21
  • @izomorphius, Sorry but I could'nt get you. Are you refering to an Application or a particular setting/config? Thanks. – Cool_Coder Jan 09 '13 at 13:24
  • 1
    There are several tool I have seen that plug in during the build of your application. What you need to do is to simply add an include in your source code and the tool wrapps the memory allocation calls. After the program exits the tool prints out a detailed report of the possible memory leaks. The downside of this approach is that you need to actually modify your code. – Ivaylo Strandjev Jan 09 '13 at 13:27
  • 1
    @izomorphius, as I am also developing some software for myself(private to me) & I am using Qt for that sake. So could you please list a few tools that i could plug into my application for checking memory leaks. Thanks! – Cool_Coder Jan 09 '13 at 13:47
  • You can't do such thing on ANY exe, you need some debug information in this exe. Also not every exe is compiled with visual studio. It may be compiled with mingw or intel compiler. Each compiler will have it's own tools – Kamil Klimek Jan 09 '13 at 13:52
  • @KamilKlimek, yup thats what I am talking about! I am developing an Application in Qt Creator & can perfectly create the debugable targets. & yes I am using the mingw compiler. Since I am not using VS2010 for my application, I would like to know the tools that i can use for Qt Creator. Thanks! – Cool_Coder Jan 09 '13 at 13:55
  • Qt Creator is just an IDE, it may use visual compiler as well as mingw. It is not a tool for "Qt Creator" but for mingw. I'm not sure but Qt Creator should already have memory profiler integrated (at least on Linux it does) – Kamil Klimek Jan 09 '13 at 14:14
  • In the far future when `clang` is ported to windows by google there is a good chance, that you will be able to use the `address-sanitizer` from within visual studio – Alexander Oh Dec 18 '13 at 21:22

2 Answers2

9

I suggest using visual leak detector as it have served me well several times. You may also try to use valgrind for windows (although I had little success on doing that).Dr. Memory also helped me a few times.

EDIT: also have a look here.

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Visual leak Detector is for Visual Studio 2010 & that perfectly answers my Question. In addition can you please suggest a tool for Qt Creator? Thanks a lot for the info... :) – Cool_Coder Jan 09 '13 at 13:58
  • 1
    Some people have already [struggled](http://stackoverflow.com/questions/6825376/how-to-detect-memory-leaks-in-qtcreator-on-windows) with that. Take a look at the thread – Ivaylo Strandjev Jan 09 '13 at 13:59
  • Visual Leak Detector runs for a very long time when the program exits. – Helge Klein Dec 18 '13 at 20:55
  • Visual Leak Detector installer is buggy; it worked for me on old VS versions, in VS 2013 x64 it can not find some (unknown) DLL even VS 2008, 2010, 2012 and 2013 redistributables are installed. It is not good idea to waste time with application that can not even set up and doesn't contain required DLLs in its installer package. – Vitalii Jun 25 '14 at 11:44
  • Dr Memory doesn't support x64. – StefanLundmark Aug 21 '15 at 21:57
  • @StefanLundmark I never claimed it did and I only mention it as "it saved me a few times". Note the question is from the beginning of 2013 and back then I was still 32 bit – Ivaylo Strandjev Aug 23 '15 at 06:18
4

The CRT library has its own memory leak detection mechanism. The output is not as detailed as what Visual Leak Detector gives you, but it is a lot faster than VLD (which easily runs for dozens of minutes after the program exits).

To enable CRT memory leak detection place the following at the beginning of stdafx.h (or somewhere else suitable):

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

Add the following right before the program's exit point(s):

_CrtDumpMemoryLeaks();

When _CrtDumpMemoryLeaks() is called it prints all leaked memory it can find to the output window.

More information on MSDN.

Note: When I used this I only got the less detailed output without line numbers although I had defined _CRTDBG_MAP_ALLOC right at the beginning of stdafx.h.

Helge Klein
  • 8,829
  • 8
  • 51
  • 71