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 thePATHenvironment 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.dllanddbghelp.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 invld.libduring 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 Data: 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.
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!!
Preethi: VLD does not seem to have any dependency on anything named sharedlibrary.dll. There might be some other reason for this error you are facing.
i have installed vld.
followd the procedure.
but application hangs..in the output window it show vld 1.9 installed and some dll loading information…
wat might be the problem?
Nagesh: Sorry, I am not able to deduce what is causing your application to hang only when executed with VLD.
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?
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.
Ranjith: I do not know what is the problem with your setup. Googling that error message you are getting might help.
Try copy dbghelp.dll with manifest to folder with your exe file.
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.
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.
Hmmm…. good tool but I usually use deleaker…
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?
Actually I disabled the satellite dll loading and the problem described above still occurs.
Qazz: I have not used VLD with C++/CLI. Please post your query on StackOverflow and hopefully someone there with CLI experience can help you out.
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??
Oliver: I doubt it is VS2005, but you could try with recent versions of Visual Studio.