Visual Leak Detector: Investigate Memory Leaks in Visual C++

Memory leaks are a part of life in C++. By default, Visual C++ looks for memory leaks when the program is executed in Debug mode. If any memory leaks are found when the program exits, it reports them in the Output window like this:

Detected memory leaks!
Dumping objects ->
{1372} normal block at 0x04BE1058, 136 bytes long.
Data: < 3-kt*-k        > 9C 33 2D 6B 74 2A 2D 6B C8 11 BE 04 00 00 00 00
Object dump complete.
The program '[408] Foobar.exe: Native' has exited with code 0 (0x0).

The leak information that it spits out is about memory blocks. The information that can be gleaned from this output is:

  • Memory block allocation number: 1372
  • Memory block type: normal
  • Memory block starting address: 0x04BE1058
  • Memory block size: 136 bytes
  • First 16 bytes of memory block data

This memory leak detection by Visual C++, which is always enabled in Debug mode, is very useful since it will always find a leak if it exists. Once the leak is known however, it is hard to figure out the source of the leak by examining the above information.

More probing along this line can be done using the definitions and calls from crtdbg.h. This is quite cumbersome and cannot always point out the source of the leak. Comprehensive information on this can be found in the MSDN article on Memory Leak Detection and Isolation.

Visual Leak Detector

Visual Leak Detector (VLD) is an open-source alternative to investigate these memory leaks. Using it is very simple and straightforward:

  • Download and install VLD. The installer will prompt about adding its bin path (C:\...\Visual Leak Detector\bin) to the PATH environment variable. Accept it or add it manually yourself. Either way, you will need to log out and log back in for the addition to the PATH to take effect. vld.dll and dbghelp.dll, from the bin directory need to be available on the system path.
  • Make sure the Visual C++ project is in Debug mode.
  • Add the VLD include path (C:\...\Visual Leak Detector\include) to the Include Directories of the project.
  • Add the VLD lib path (C:\...\Visual Leak Detector\lib) to the Additional Library Directories of the project.
  • Add #include <vld.h> to any of the C++ source files in the project. This header will bring in vld.lib during the linking stage.
  • Rebuild the project and execute the compiled program in Debug mode. On program exit, VLD will print out the memory leak information it detected in the Output window.

The memory leak information printed by VLD looks like this:

---------- Block 1199 at 0x04BE1058: 136 bytes ----------
Call Stack:
d:\Foobar\FooLog.cpp (26): FooLog::getInstance
d:\Foobar\FooMain.cpp (75): FooMain::init
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): __tmainCRTStartup
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): WinMainCRTStartup
0x759A3677 (File and line number not available): BaseThreadInitThunk
0x770C9D42 (File and line number not available): RtlInitializeExceptionChain
0x770C9D15 (File and line number not available): RtlInitializeExceptionChain
9C 33 2D 6B    74 2A 2D 6B    C8 11 BE 04    00 00 00 00     .3-kt*-k ........
00 00 00 00    70 14 BB 6C    70 14 BB 6C    00 00 00 00     ....p..l p..l....
00 00 00 00    68 14 BB 6C    68 14 BB 6C    00 00 00 00     ....h..l h..l....
00 00 00 00    6C 14 BB 6C    6C 14 BB 6C    20 12 BE 04     ....l..l l..l....
00 00 00 00    CD 00 CD CD    00 00 00 00    01 CD CD CD     ........ ........
68 14 BB 6C    78 33 2D 6B    00 00 00 00    00 00 00 00     h..lx3-k ........
00 00 00 00    01 02 00 00    06 00 00 00    00 00 00 00     ........ ........
00 00 00 00    00 00 00 00    88 11 BE 04    5C 10 BE 04     ........ ....\...
00 00 00 00    20 CD CD CD                                   ........ ........

We can note the following information from this output:

  • The block allocation number output by Visual C++ leak detection and by VLD do not necessarily match.
  • The memory block address and size information of Visual C++ and VLD match. This confirms that this VLD information is about the same memory block as that reported by Visual C++. This is useful when the program has multiple memory leaks. The programmer can then match each of the Visual C++ memory leak to the corresponding VLD memory leak information.
  • VLD provides a lot of information: the entire call stack trace with function call names, source code filenames and line numbers. It also prints out a lot more data from the memory block.

By looking at the sourecode filename and the line number in it, the programmer should be able to get solid leads to the memory leak and hopefully be able to fix it. 🙂

31 thoughts on “Visual Leak Detector: Investigate Memory Leaks in Visual C++

  1. hi,
    im using the VLD for the first time. i have done the installation as per stated above and have included the header file vld.h in the source code
    but every time i run the debug of my application, the error pops up saying the sharedlibrary.dll cant be opened.
    please help!!


  2. i have installed vld.
    followd the procedure.
    but application the output window it show vld 1.9 installed and some dll loading information…
    wat might be the problem?


  3. I am trying Vld 2.2 on x64. Everything compiles fine, but when I run the application I get an error “The application was unable to start correctly (0xc0150002)”. Any idea?


    1. Dpendency walker says:

      Error: The Side-by-Side configuration information for “\x64\debug\VLD_X64.DLL” contains errors. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail (14001).
      Warning: At least one delay-load dependency module was not found.
      Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.


  4. Hi, I have installed 1.9 as instructed. but it prompts “This application has failed to start because vld.dll was not found. Re-installing the application may fix this problem”. I checked the Path, there was the bin path, I saw the vld.dll there. I don’t know what to do.


    1. Sheng: There are a couple of things you can try. First, see if it works once you log out and login. If not, just place the vld.dll in the same directory as the exe file.


  5. I’m trying to use VLD 2.2.3 (latest version so far) for a solution with UI in WPF, a C++/CLI middle-ware and a native DLL engine with more auxiliary dlls/libs. I placed the #include into the stdafx.h file from the engine dll. I also modified the vld.ini ( ReportTo = both ).

    Under those circumstances, after successfully running the app in debug, an empty “memory_leak_report.txt” file is created beside the executable, and no vld related info is displayed in the output window of VS 2010.

    I also created a test solution with the same architecture as above. VLD in this logs the leaks ok to the file, but in the output there is still no related info.

    Would the fact that, engine dll has an dynamically loaded satellite dll (using ::GetProcAddress fct) can influence the VLD functionality?


    1. Actually I disabled the satellite dll loading and the problem described above still occurs.


  6. hi, I installed VLD 1.9d, it looks like everything is fine, I debug application at the end I see “WARNING: Visual Leak Detector detected memory leaks!”
    but after that there is just block address but not sourcecode filename or line number…

    any idea where could be the problem? is it that I’m running VS2005??


  7. This tool doesn’t work to me. I run it in VS 2010. It says “No memory leaks detected”. Meanwhile, i detect that there are memory leaks because i use this project as a reference to another one and there are shown some memory leaks. In addition to that, I also try to use _CrtDumpMemoryLeaks and it shows me memory leaks too, but not positioning and lines. I also tried to test it by adding this line of code char *ptrTest = new char[500]; and it doesnt detect. Anyway, is there any solution for that?


    1. Ola: I have not used this tool for a long time now, so no idea how to make it work again. Sorry about that.


    2. Thank you Ash. Don’t worry. I already found another solution, I did the overloading of new and delete operators and it works fine now :).


  8. “” fatal error C1083: Cannot open include file: ‘vld.h’: No such file or directory “”
    i got that above error..and i was followed above points to add a .lib , bin and all paths respectively..and i have given #include in my .cpp class..then i was done debugging..Please help me out ..


    1. Raj: The error indicates that the compiler is not able to find the header file. Maybe you should check if VLD is in the include directories again.


  9. hi.i want to check the memory leak due to the use of SysAllocString,SysReAllocString ect. but it can’t work well and programme crashes.i guess it may be caused by the multithreading. so i wanna ask if there is any thread-safe aspect that i should consider? thank you.


  10. Hi,
    Trying the version 2.4 on a DLL, and remote debugging.
    I did not get any report, neither NO LEAK or DETECTED MEMORY LEAK.

    I explicitly tried to put memory leaks in the code to check my setup, but even that did not catch.
    Am i missing anything?


  11. I observe that the application takes enormously large time than the normal functioning of the exe when used with visual leak detector. I understand it will normally take more time, but this is taking really more time, Does it depend on the usage of ppl libraries or something??


  12. I install Visual Leak Detector.
    For a test I do

    char * stest = new char[10];

    with no delete and I see the leak reported.

    Now I add:

    strcpy(stest, “this is a test”);

    but see no report of this overrun. Should I?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.