Use of the VLD (Visual leakdetector) memory leak Library

Source: Internet
Author: User
Tags winsxs folder

VLD Introduction
    Due to the lack of the so-called garbage collector, the allocation and release of memory need to be controlled by the programmer, which will bring some difficulties to C + + programmers. As your program becomes more complex, its memory management becomes more and more difficult. Memory leaks and memory out-of-bounds are among the most common memory problems.    If the memory leak is not very serious, the program will not have too much effect in a short time, and when the process terminates, all allocated memory will be released. But for long-running programs, the damage is staggering, falling from performance to memory exhaustion, and even affecting the normal operation of other programs.    In addition, the memory problem has a common feature, it does not have a very obvious phenomenon, when there is an anomaly, it is difficult to check the cause of the problem, which makes debugging memory problems with great difficulty. The VLD is a free memory leak checking tool for visualc++.  Can be found on the website, compared to other memory leaks oh give you according to, he in check memory leak colleague, also has the following features:    1 can get the memory leak point of the call stack, if possible, you can also get the file and line number;    2) Can get complete data of leaking memory;    3) The level of malicious set memory leak report,    4) It is provided in the form of a dynamic library without compiling the source code and requires only a small change program;    5) The source code is published with the GNU License and has detailed documentation and comments.    from the point of view of use, VLD is easy to use, and the only thing that needs to be modified in the user's own code is the VLD header file, which runs its own program to discover memory problems. From the research point of view, if the input to the VLD source code, you can learn the heap memory fragmentation and release of the principle of memory check the principle of machine memory operation of common skills and so on.
VLD use
    VLD website: Visual-leak-detector-enhanced-memory-leak-detectio download Visual Leakdetector, current version 2.2.3, open the Visual C + + IDE "Tools" → "Options" → " Projects and solutions → VC + + directory, add VLD header file path "\include" path in "Include file", add "\lib\win32" path of VLD library file in "library file", and "\bin\win32" of Dynamic Library The path has been added to the environment variable at the time of installation, and if not added, you will need to manually copy the files under "\bin\win32" to the directory where the executables are located (the copied files are DBGHELP.DLL/MICROSOFT.DTFW.DHL.MANIFEST/VLD _x86.dll/vld.ini). Next you need to add the VLD to your own code. The method is simple, as long as you include Vld.h in the. cpp file that contains the entry function.    If the CPP file contains stdafx.h, the statement that contains the vld.h is placed after the stdafx.h containing statement, otherwise it is placed at the front. Sample program: #include <vld.h>                 //header file with VLD #include<stdlib.h> #include <stdio.h>void f () {    int *p = new Int ( 0x12345678);    printf ("p=%08x,", p);}    int main () {    F ();    return 0;    Note: VLD can only be used under Windows, adding precompilation options when the Vld.h header file is included.    Note: In release mode, Visualleak Detector is not linked. Note: Visual Leakdetector has some configuration items that you canTo set the memory leak report (file, debugger), copy the Vld.ini file under the "\visual Leak Detector" path to the directory where the executable file is located (run in the IDE, copy to the project directory), modify the following: Reportfile =.\ Memory_leak_report.txt Reportto = Both
VLD tool principle
    Let's take a look at how VLD works. In visualc++, the tool CRT Debug heap tool is built in, and it records the file name and line number of the memory allocated in the memory block when the debug version is used to allocate memory. When the program exits, the CRT will do some cleanup work when the main function returns, check the debug heap memory, and if there is still memory not released, there must be a memory leak problem. The file name and line number can be obtained from the header of these memory blocks that are not freed. This static method can check for a memory leak, but does not know how the leak happened, or how the memory allocation statement was executed, and want to understand that the memory allocation process must be dynamically tracked. VLD does this by recording its context each time the memory is allocated, and when the program exits, it finds its contextual information for the detected memory leak and translates it into output to the report.
    VLD to record each memory allocation, it monitors the allocation of debug heap memory through the allocation hooks provided by Windows allocation hooks. It is a user-defined callback function that is called before each memory is allocated from the heap, and is VLD using _crtsetallocation to register the hook function at initialization. Global variables are initialized when the program is initialized, and if VLD as a global variable can be started with the program, but C + + does not contract the order in which the global variables are initialized, and if there is memory allocation in the constructors of other global variables, it may not be detected. As a result, VLD uses the #pragma init_seg provided by C + + to reduce the initialization of other global variables before it. According to the definition of #pragma init_seg, the global variable initialization is divided into 3 stages, the first is the compiler stage, the C language runtime library is generally initialized; then the Lib segment, which is generally used for the initialization of the third-party class library relate; Finally, the user segment, Most of the initialization is done at this stage.
Log Memory Allocations
    A memory allocation hook function needs to have the following definitions: int allochook (int alloctype, void*userdata, size_t size,int blocktype, long RequestNumber, Onst unsigned char*filename, int linenumber);    The function needs to be registered at VLD initialization, and each time it is called before allocating memory from the heap, the thing it needs to deal with IS to record the call stack at this time and the unique identity requestnumber of the heap memory allocation. Getting the binary representation of the current stack is not very complicated, but because different architectures, different compilers, and different operating systems produce stack content that is not the same, it is more complicated to interpret the stack and get the entire function's call process. However, Windows provides a StackWalk64 function to get the contents of the stack.     VLD is a commonly used C + + memory leak checking tool that can be used in viusalc++, and is used in Viusal Studio 2008 and 2010 to be aware of two points:    1) Version issue: VLD has been updated to version 2.2, fixed a number of bugs, and in the 2010 version of Good work, visualc++ 6.0 recommended 1.0 version, 1.9b version is not very stable deprecated, 2.2 version of the download URL is http://    2) Settings change: vc++directories settings have changed position, in 2010 the process is set as follows: View | Other Window | Property Managergo to "VC + + Directories" Settingsset include folder pathset lib folder path    point OK, we set the include and Lib directories.
Use problem 1:VLD 1.9
    Use of VLD under Vista, always error does not work, later after searching, in  The solution was found in the comments on visualleakdetector.aspx: Comments "solution forrunning 1.9 Beta on Visual Studio-with Vista" gives a workaround: Comments 1:VLD Keptcrashing when trying to use 1.9g beta on Windows Vista, Visual Studio 2008. Itried all the suggestions in here and nothing worked. But I-finally figured itout.when you do a project in Visual C + + 2008,it sets some strange advanced Linker properties tha T cause VLD to crash:i changedlinker->advanced->randomized Base Address from Enable Image randomization (/dynamicbas E) to Disable Image randomization (/dynamicbase:no) then I changed linker->advanced->dataexecution prevention from I Mage is compatible with DEP (/NXCOMPAT) to Defaultand now it works perfectlyplease let me know if this helped you!  It ' ll make me feel better for spending a whole day trying to get it Working! -nadav comments 2:the Base Address randomization seems to benot necessary.   Just disable DEP. The general meaning is to say that only theTo disable DEP, in the project properties, linker, advanced, Data Execution Prevention (DEP), set to default or image-to-DEP incompatible (/nxcompat:no).  (It seems to be unavailable after modification.) Note: This option is only valid for Vista!!!
Question 2:vld 2.2.3
    Visual leak detector was used in the project, and the program failed to start the error "Application Normal startup failure (0xc0150002)" when debugging.    resolution process:    view vs Output information The last article is: Theprogram ' [3980] MobileSignalAnalyzer.exe:Native ' has exited with code-1072365566 ( 0xc0150002)    on-line multi-party search has: /article/details/6685724    These two articles have a solution, is to copy the two VLD in the execution folder under the normal Microsoft.VC90.CRT.manifestMicrosoft.DTfW.DHL.manifest    cause:    VC2003, VC2005, VC2008 and subsequent versions, the most basic of the underlying CRT, MFC, ATL Library are reconstructed, in order to avoid different versions of the library caused conflicts, the reconstructed library files are generally placed in the C://windows/winsxs folder, and with a specific folder/file name to identify;    Unlike VC6, VC2003, VC2005, VC2008, and later versions, introduce the concept of manifest inventory, where the application compiles a corresponding. manifest file at the same time, and compile the. manifest file as a resource into a DLL or EXE. The. manifest file is actually a text file in XML format that records the version and name of the CRT, MFC, ATL Library to be referenced in the DLL or EXE. VC6 compiled applications are direct calls to the CRT, MFC, ATL DLLs, and VC2003, VC2005, and VC2008 compiled programs that query the records compiled into manifest in the resource, and then follow the version and name provided by the record to search for the corresponding CRT, MFC, the ATL library, and the. manifest file that is published with the library, the search path includes the current directory, C://windows/winsxs, and so on, if the corresponding library file is not found, then "application normal initialization failed."

VLD (Visual leakdetector) memory leak library use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.