Using Visual Leak Detector


This section briefly describes the basics of using Visual Leak Detector (VLD).

Important! : Before using VLD with any Visual C++ project, you must first add the Visual Leak Detector include and library directories to the Visual C++ include and library directory search paths:
For all compiler versions take care to ensure that no junk characters get added when you add the include and library paths. If you browse to the "Program Files(x86) folder using the dialog box provided by Visual Studio and select it you could end up seeing the "%" replacing the "(".

And remember to close and open the Visual Studio IDE once you have modified the default include and library paths which the compiler and linker would always look at.
  • Visual C++ 2010/2012/2013: Go to View ->Property Manager, select Microsoft.Cpp.Win32.user. Select VC++ Directories and then "Include files" from the tree. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib\Win32 subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list. Repeat for Microsoft.Cpp.x64.user, but select lib\Win64 subdirectory instead.
  • Visual C++ 2005 and 2008: Go to Tools -> Options -> Projects and Solutions -> VC++ Directories. Select "Include files" from the "Show Directories For" drop-down menu. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib\Win32 subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
  • Visual C++ 2003: Go to Project Properties -> C/C++ -> General -> Additional Include Directories and add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select Additional Library Directories and add the lib\Win32 subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.


To use VLD with your project, follow these simple steps:
  1. In at least one C/C++ source file from your program, include the vld.h header file. It should not matter which file you add the include statement to. It also should not matter in what order the header is included in relation to other headers. The only exception is stdafx.h (or any other precompiled header). A precompiled header, such as stdafx.h, must always be the first header included in a source file, so vld.h must be included after any precompiled headers.
  2. If your program contains one or more DLLs that you would also like to check for memory leaks, then also include vld.h in at least one source file from each DLL to be included in leak detection.
  3. Build the debug version of your program.

Note: Unlike earlier (pre-1.9) versions of VLD, it is now acceptable to include vld.h in every source file, or to include it in a common header that is included by many or all source files. Only one copy of the VLD code will be loaded into the process, regardless of how many source files include vld.h.
VLD will detect memory leaks in your program whenever you run the debug version. When you run the program under the Visual C++ debugger, a report of all the memory leaks detected will be displayed in the debugger's output window when your program exits (the report can optionally be saved to a file instead, see ReportFile under Configuration Options). Double-clicking on a source file's line number in the memory leak report will take you to that file and line in the editor window, allowing easy navigation of the code path leading up to the allocation that resulted in the memory leak.

Note: When you build release versions of your program, VLD will not be linked into the executable. So it is safe to leave vld.h included in your source files when doing release builds. Doing so will not result in any performance degradation or any other undesirable overhead.

Last edited Dec 25, 2013 at 4:20 PM by KindDragon, version 7

Comments

yudiannlu Wed at 10:42 PM 
I had a project run on debug version and had memory leak issue:
So VLD was installed on VS2015 - and run with Remote debugger
After add "vld.h" into project, but end up "exception" and could not pin point the memory leak....
Please help.....

'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\GyroDRAccTest.exe'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\GyroDRACC.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\user32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\gdi32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\lpk.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\usp10.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\vld_x86.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\advapi32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\sechost.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\dbghelp.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\MSVCP140D.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\VCRUNTIME140D.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\ucrtbased.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-timezone-l1-1-0.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l2-1-0.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-localization-l1-2-0.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-synch-l1-2-0.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-processthreads-l1-1-1.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l1-2-0.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\ws2_32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\nsi.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\mfc140ud.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\ole32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\oleaut32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\shlwapi.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\msctf.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\uxtheme.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2\comctl32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.17514_none_72d18a4386696c80\GdiPlus.dll'. Symbols loaded.
Visual Leak Detector read settings from: (default settings)
Visual Leak Detector Version 2.5.1 installed.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\cryptbase.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\shell32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Program Files\Common Files\microsoft shared\ink\tiptsf.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\dwmapi.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded '\\TRAPEZE_V801\ProjectArea\AppTest\Debug\zTrace32.DLL'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\comdlg32.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\clbcatq.dll'. Symbols loaded.
24 c5 00 00 3b 0d
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Windows\System32\oleacc.dll'. Symbols loaded.
'GyroDRAccTest.exe' (Win32): Loaded 'C:\Program Files\UPDD\TBHOOK.DLL'. Module was built without symbols.
24 c1 00 00 3f 0d
24 c1 00 00 3f 0d
24 d9 00 01 01 25 0d
24 d7 00 00 29 0d
24 d5 00 01 01 29 0d
24 d5 00 01 00 2a 0d
24 e5 00 01 00 1a 0d
The thread 0x113c has exited with code 0 (0x0).
The thread 0x17fc has exited with code 0 (0x0).
The thread 0x46c has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
{577} normal block at 0x004E0550, 32 bytes long.
Data: < O > 07 CD CD CD 00 00 00 00 00 00 4F 00 CD CD CD CD
{571} normal block at 0x004E0280, 32 bytes long.
Data: < O > 07 CD CD CD 00 00 00 00 00 00 4F 00 CD CD CD CD
{570} normal block at 0x004EB530, 64 bytes long.
Data: < N N p N N > D0 02 4E 00 20 03 4E 00 70 03 4E 00 C0 03 4E 00
{564} normal block at 0x004E0500, 32 bytes long.
Data: < N > 07 CD CD CD 00 00 00 00 00 00 4E 00 CD CD CD CD
{557} normal block at 0x004E95A8, 12 bytes long.
Data: < ~c > 10 D0 7E 63 14 01 00 00 14 01 00 00
{554} normal block at 0x004E04B0, 32 bytes long.
Data: < L > 07 CD CD CD 00 00 00 00 00 00 4C 00 CD CD CD CD
{548} normal block at 0x004E0460, 32 bytes long.
Data: < L > 07 CD CD CD 00 00 00 00 00 00 4C 00 CD CD CD CD
{539} normal block at 0x004E03C0, 32 bytes long.
Data: < L > 07 CD CD CD 00 00 00 00 00 00 4C 00 CD CD CD CD
{533} normal block at 0x004E0370, 32 bytes long.
Data: < N > 07 CD CD CD 00 00 00 00 00 00 4E 00 CD CD CD CD
{527} normal block at 0x004E0320, 32 bytes long.
Data: < O > 07 CD CD CD 00 00 00 00 00 00 4F 00 CD CD CD CD
{521} normal block at 0x004E02D0, 32 bytes long.
Data: < P > 07 CD CD CD 00 00 00 00 00 00 50 00 CD CD CD CD
{448} normal block at 0x004EB428, 216 bytes long.
Data: <H ~c KN > 48 0B 7E 63 90 4B 4E 00 FF FF FF FF 00 00 00 00
{267} normal block at 0x004EC058, 48 bytes long.
Data: <z T r a c e 2 > 7A 00 54 00 72 00 61 00 63 00 65 00 20 00 32 00
{264} normal block at 0x004D6620, 8 bytes long.
Data: < > B4 FC F0 01 00 00 00 00
{130} normal block at 0x004D6498, 8 bytes long.
Data: < ~c > 88 D0 7E 63 00 00 00 00
{129} normal block at 0x004D6460, 8 bytes long.
Data: <t ~c > 74 D0 7E 63 00 00 00 00
{128} normal block at 0x004D6428, 12 bytes long.
Data: <(dM (dM > 28 64 4D 00 28 64 4D 00 CD CD CD CD
{127} normal block at 0x004D63F0, 8 bytes long.
Data: <\ ~c > 5C D0 7E 63 00 00 00 00
Object dump complete.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 144 at 0x004E95A8: 12 bytes ----------
Leak Hash: 0x066C1818, Count: 1, Total 12 bytes
Call Stack (TID 4792):
Exception thrown at 0x62727EAC (dbghelp.dll) in GyroDRAccTest.exe: 0xC0000005: Access violation reading location 0x03038B28.

tlguo Mar 21 at 11:35 PM 
I have a native DLL which is loaded by a C# managed program and I suspect memory leaks in the native DLL. How can I use VLD to detect in memory leak in the native DLL? That native DLL allocates memorys after the function InitModule() and is supposed to withdraw all memory in calling unloadModule(). I insert VLDEnable() at the begining of initModule() function of my native DLL and VLDReportLeaks() at the end of the unloadModule() but I still can't see any report message in the output panel. Why?

BubuS24 Apr 24, 2015 at 10:16 AM 
Would be nice to have the VLD version accessible at program compiletime.
I'm currently using VLD 2.4rc2 with VC++ 2013. Any means available?

andrewjturner Jan 12, 2015 at 8:03 AM 
Debugging my DLL running under Excel 2010 under VS2008 Express. After installing VLD on VS2008 Expresss, when I debug by opening the Excel sheet and loading my DLL, I now get endless Unhandled Exception pop-ups in the debuigger which prevent me from continuing - I can only Continue (up pops another) or break. When I remove VLD, the problem goes away. Anyone any ideas please?

cvolpe Sep 2, 2014 at 3:31 PM 
I installed without a problem. I created a dummy bare-bones 64-bit console application using VS2013 on Windows7. I added only three lines to the main program file created by the wizard. The resulting code looks like this:

// TestVLD.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vld.h>


int _tmain(int argc, _TCHAR* argv[])
{
char *testLeak = new char[1000000];
testLeak = nullptr;
return 0;
}

When I run in the debugger, the PDB files for the main program load fine, but there are a bunch of "Cannot find or open the PDB file" messages pertaining to MS built-in OS files in C:\Windows\System32. After those messages, the only report I get from VLD is the following:

Visual Leak Detector Version 2.3 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
The program '[7840] TestVLD.exe' has exited with code 0 (0x0).

I would have thought that VLD would have no problem detecting that blatant memory leak. What am I doing wrong?

DTang33 Aug 12, 2014 at 12:08 AM 
Hi all, I'm new to VLD. I'm debugging an application with known memory leak (~20 M). I just installed Visual Leak Detector, configured vld with all required setting, and ran it with my program. However, I did not get any report of memory leak from Visual Studio Output; instead, I found VLD module could not function due to following warning from the Visual Studio Output:
WARNING: Visual Leak Detector: A module, igdumdim32.dll, included in memory leak detection
does not have any debugging symbols available, or they could not be located.
Function names and/or line numbers for this module may not be available.

Any thought how I can fix this problem? Thanks.

123456 Mar 25, 2014 at 10:32 AM 
I was getting the error 0xc0150002, copying the files dbghelp.dll and Microsoft.DTfW.DHL.manifest from the dbghelp folder to the bin folder solved the problem.
Nice tool, thanks!

brewmanz Feb 5, 2014 at 9:04 PM 
I'm using VS2012 with Win7(x64) and having some issues setting VLD up:
- Initial installation went fine.
- "View ->Property Manager" needed "View ->Other Windows -> Property Manager"
- "select Microsoft.Cpp.Win32.user" I have absolutely no idea where or how to do this. Property Manager is an empty form saying "No Visual C++ project is loaded" and its Properties (only active part I have found) says "There are no property pages for the selection."

polomora Nov 12, 2013 at 9:12 AM 
Since my last comment I've started using VLD with VS2012. Compared with VS2008, the time to shutdown our application (when VLD does its magic to collate any outstanding memory leaks) has lengthened enormously, from about 2 minutes to about 30 minutes. When VLD is disabled via the vld.ini config file, the problem disappears, so this confirms that VLD is the problem.

polomora Jul 2, 2013 at 3:23 PM 
On further use, I noticed that the summary total of the number of bytes leaked reported by VLD does not agree with the sum of the reported leaked bytes. The summary number of leaks does agree.

I tested this using the following options:
[Options]
VLD = on
AggregateDuplicates = no
ForceIncludeModules =
MaxDataDump = 32
MaxTraceFrames =
ReportEncoding = ascii
ReportFile = W:\Main\EnsorApplication\Castor\Build\VC90NT\Castor\Win32\Debug\vld_leak_report.txt
ReportTo = file
SelfTest = off
StackWalkMethod = fast
StartDisabled = no
TraceInternalFrames = no
SkipHeapFreeLeaks = no

At the end of the generated report:
Visual Leak Detector detected 11470 memory leaks (774260 bytes).

When I use scripts to calculate these figures for myself
cat /cygdrive/c/Users/21ppm/Desktop/vld_leak_report.txt | grep "\-\- Block" | wc -l
11470 (correct)
cat /cygdrive/c/Users/21ppm/Desktop/vld_leak_report.txt | grep "\-\- Block" | cut -f2 -d':' | cut -f2 -d' ' | awk 'a+=$1; END{print a}'
361340 (reported total was 774260)

polomora Jun 27, 2013 at 7:19 AM 
Also registered with CodePlex just to thank you for such a useful tool. I used it with VS2008, and it worked like a charm.

One thing that is unclear in the Documentation -> Configuration Options section. This section states that a a local configuration of VLD can be configured by copying a version of the vld.ini configuration file to the "program's working directory". Is this the directory where the executable program and (optiionally) its DLLs are located? I tried this, and it didn't work. So i've had to configure VLD using the master copy of vld.ini in the VLD installation directory.

andersmw May 3, 2013 at 7:46 PM 
the "failed to initialize properly" error was not cured for me.. I feel I've been __declspec-ed.

JPDworkin Mar 9, 2013 at 7:35 AM 
After a little fiddling I was able to get your tool working and found my leak problem. A very nice job. Here are the issue I found for my application/installation so you can improve the tool and documentation:
1 - I work at a large corporation with strict policies. When I tried to run my application with VLD enabled, I got an 'application failed to initialize properly' error on startup. This was finally cured by using: CACLS "C:\software\Visual Leak Detector\bin" /E /G BUILTIN\Users:R on the command line.

2 - The report did not reliably appear unless I explicitly added a call to VLDEnable in my main InitInstance and calling VLDReportLeaks as another commenter posted above.

raananb Feb 23, 2013 at 7:38 AM 
The instructions for Visual C++ 2010 do not apply as such to Visual C++ 2010 Express.
Some modifications would help:
1. In Properties management, you must select 'Debug | Win32' (in my case) before you can select 'VC++ Directories'.
2. Once VC++ Directories is selected, the wording '"include file" from the tree' is imprecise: replace with "add VLD/Include in 'Include Directories'".

3axap Nov 23, 2012 at 11:58 AM 
You may need to add VLDEnable() function call somewhere in your code to enable the detector or VLDReportLeaks() to create a report. Though the report will appear only when application exits.

mjrtstr Nov 7, 2012 at 1:42 AM 
Installed vld, no problem. Specifically set a function to cause a memory leak. Within that .cpp file, included <vld.h>. Called the known memory leaking function multiple times. Exited the program. NOTHING IN THE DEBUG OUTPUT WINDOW WHATSOEVER. What am I missing?

InsideOutCloud Jun 8, 2012 at 3:56 PM 
Just as a note, the setup directions for Visual Studio 2010 also work for Visual Studio 11