2011年11月11日 星期五

[VC] Use VLD for memory leak detection

Allocating and releasing memory is a big headache for many Visual C++ programmer.
Usually, the memory is allocated and forget to be released.
It will make the program unstable, can't run properly, or occupy more and more system resources, the worst case is make the system can't response anymore.
There are many tools helping programmer to detect Memory Leak, such as Rational Purify, BoundersChecker...etc, but they are not cheap.

Here is the introduction of a free Memory Leak detection tool: VLD (VisualLeak Detector).

I would rather take VLD as a library for programmer than a tool.
For programmers, it's easy to add memory leak detection to source code instead of understanding complicated operation of Purify or BounderChecker.
For developing a  medium or small program, I think it's quite enough. :-)

The following are the description of installing and using VLD.
VLD Version:  2.2
Official Website:  http://vld.codeplex.com/

Download link:  http://vld.codeplex.com/releases/view/70398

  • Click setup.exe of VLD to start installation. Here is the installation of Version 2.2


  • The default location of VLD will be installed to Program Files.

  • When installing, VLD will ask you to add the location of VLD dll files to your "Path" environment variable. Click Yes to continue.

  • Installation finished. It's very easy to install. :-)

  • Then, open your VC project. Add VLD include path in VC project property.

  • Add VLD library path to VC project property. I added Win32 since my OS is 32 Bit. If your OS is 64 Bit, please change VLD library path to Win64.

  • Include vld.h in the cpp file which wants to be detected for memory leak. We can include vld.h in Main dialog or App if we want to check whole program.


  • OK, let's test VLD.
    Allocate a memory and don't free it in purpose, run Debug to see how VLD report memory leak.

  • When the program ended, VLD reported that memory leak was detected in Output window. Double click at the line with line number of source code will jump to the location. It's very convenient.

  • The data in the memory not freed listed in Output window in detail. It's also helpful for debugging.

Afterword: VLD is really helpful for VC programmer. But I had the experience that my program can't be launched when using VLD. And then I changed to open VLD only when I want to detect memory leak. VLD is closed when memory leak detection is finished.

Reference:
Memory Leak Detection in C++
Memory leak tool for C++ under Windows

沒有留言:

張貼留言