Memory Leak Detection in Visual Studio

来源:互联网 发布:python adb命令 编辑:程序博客网 时间:2024/06/10 20:12

1.     Overview

Memory leak—the failure to properly deallocate memory that was previously allocated.

The Visual Studio debugger and C run-time (CRT) libraries provide effective means for detecting and identifying memory leaks. The primary tools for detecting memory leaks are the debugger and the C Run-Time Libraries (CRT) debug heap functions.

2.     How to use

To enable the debug heap functions, we need to include the following statements in our program:

#define _CRTDBG_MAP_ALLOC  // a debug info trigger

#include <stdlib.h>

#include <crtdbg.h>

 

By including crtdbg.h, you map the malloc and free functions to their debug versions, _malloc_dbg and _free_dbg, which keep track of memory allocation and deallocation. This mapping occurs only in a debug build (in which _DEBUG is defined). Release builds use the ordinary malloc and free functions. Wrapping up the memory allocate and free function is a common way to implement a memory management system.

The #define statement maps the base versions of the CRT heap functions to the corresponding debug versions. You do not absolutely need this statement, but without it, the memory leak dump will contain less useful information.

Once you have added the previous statements, you can dump memory leak information by calling the fuction:

_CrtDumpMemoryLeaks();

The function displays memory leak information in the Output window.

Here is an example source code:

#include "stdafx.h"

 

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#include <malloc.h>

#include <string.h>

 

int main(int argc, char* argv[])

{

    _crtDbgFlag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF;

 

    char *p1, *p2;

    // _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);

 

    p2=(char *)malloc(100);

    strcpy(p2, "memory leak detected!");

    p1 = new char[20];

 

    return 0;

}

 

3.     Dumping message

Here is the dumping message in the output window when the main function reutrned in debug run mode:

Detected memory leaks!

Dumping objects ->

f:/program files/microsoft visual studio 8/vc/include/crtdbg.h(1150) : {57} normal block at 0x00382F30, 20 bytes long.

 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD

f:/advanced windows/windowsdebug/memleak/memleak.cpp(20) : {56} normal block at 0x00382E90, 100 bytes long.

 Data: <memory leak dete> 6D 65 6D 6F 72 79 20 6C 65 61 6B 20 64 65 74 65

Object dump complete.

Now let’s examine the dumping message:

·                The source file name indicates where the leaked memory was allocated. The number in parentheses indicates line number within the file.

·                The memory allocation number (inside the curly braces).

·                The block type, which is normal, client, or CRT.

·                The memory location in hexadecimal form.

·                The size of the block in bytes.

·                The contents of the first 16 bytes, also in hexadecimal form.

We can not see where the when new operator is used, the dumping message only shows crtdbg.h

 

4.     Memory Block Type

By Enabling Memory Leak Detection, the memory leak information identifies each block of leaked memory as a normal block, a client block, or a CRT block. In practice, normal blocks and client blocks are the only types you are likely to see.

·                A normal block is ordinary memory allocated by your program.

·                A client block is a special type of memory block used by MFC programs for objects that require a destructor. The MFC new operation creates either a normal block or a client block, as appropriate for the object being created.

·                A CRT block is a block of memory allocated by the CRT library for its own use.

 

5.     Breakpoints on a Memory Allocation

We can set Breakpoints on a Memory Allocation Number, and in this way we can detect where the memory allocation happen by examine the call stack window.

There are two ways to set the breakpoint:

Firstly, set a memory-allocation breakpoint in the Watch window by input a variable name: _crtBreakAlloc, then set the value to the Memory Allocation Number.

If you are using the multithreaded DLL version of the CRT library (the /MD option), include the context operator, as shown here:

{,,msvcr71d.dll}_crtBreakAlloc

But according to my testing, we can’t break at some early memory allocation. I guess that it’s the CRT lib to allocate memory, by then the memDBG lib haven’t started yet.

Alternately, you can use the _CrtSetBreakAlloc() function, which has the same effect.

原创粉丝点击