Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / C++

Detecting Memory Leaks using the CrtDbg Library

Rate me:
Please Sign up or sign in to vote.
4.52/5 (19 votes)
19 Mar 2010CPOL5 min read 88.8K   1.1K   50   27
The CrtDbg Library provides APIs which can be used to detect memory leaks

Image 1

Abstract

Memory is a very important aspect for an application to run. Applications that leak large amounts of memory or leak progressively may display symptoms ranging from poor (and gradually decreasing) performance to running out of memory completely. Worse, a leaking program may use up so much memory that it causes another program to fail, leaving the user with no clue to where the problem truly lies. In addition, even harmless memory leaks may be symptomatic of other problems.

When an application is very big, it could have 100+ files and 1000+ functions and it would be tough to find memory leaks in the application, so we can use the CrtDbg Library APIs to find the memory leaks in that application.

For memory leak detection, we have so many tools available in the market, but you need to pay a lot for them. This article talks about a C Runtime Library which is free and available with all Windows OSs, and is called the CrtDbg Library. This library provides APIs which can be used to detect memory leaks.

Audience

Win32/Win64 developers who want to detect memory leaks in their applications.

Memory Leak Samples

C++
// 1.
class test
{
  public: test()
  {
     a = new int;
  }
 ~test()
 {
    delete a;
 }
private:
  int *a;
};

test *t = new test[10];
// When we are not calling delete t then destructor will not be called 
// and hence this code is having memory leak.

// 2.
{
  // no delete called for str having 20 bytes allocated
  char* str = new char[20];
  strcpy(str,"memory leak");
}

Introduction

Memory Allocation on Stack

C++
int number = 10;

Whenever a number variable goes out of scope, the memory allocated for the variable will be deallocated automatically.

Memory Allocation on Heap

C++
int * number = new int[10];

The above line allocates 10 integer sizes on the heap (40 bytes if the integer size is 4 bytes), and we have to call the delete function to deallocate the memory allocated on the heap. delete [] number is required to delete the memory allocated on the heap in the above example. Sometimes, we forget to call delete for the variables allocated on the heap, and we can use C Runtime Library (CRT) functions to find the leaks in a given code.

Using the Code

We can use CrtDbg library functions to detect the memory leaks in our application.

The technique for locating memory leaks involves taking snapshots of the application's memory state at key points. The CRT library provides a structure type, _CrtMemState, which you can use to store a snapshot of the memory state:

C++
_CrtMemState s1, s2, s3;

To take a snapshot of the memory state at a given point, pass a _CrtMemState structure to the _CrtMemCheckpoint function. This function fills in the structure with a snapshot of the current memory state:

C++
_CrtMemCheckpoint(&s1);

After calling _CrtMemCheckpoint(&s1), code can be written in which we can detect the memory leak. (_CrtMemCheckpoint can be used anywhere in the code.)

After writing your code, use _CrtMemCheckpoint( &s2 ) to take another snapshot of the memory at that time; after that, you can call _CrtMemDifference(&s3, &s1, &s2).

_CrtMemDifference compares two memory states s1 and s2, and returns their differences (debug version only). _CrtMemDifference(&s3, &s1, &s2) will return 0 if there is no memory leak between the s1 and s2 memory checkpoints, and returns 1 if the code written between the checkpoints s1 and s2 has a memory leak.

If _CrtMemDifference returns 1, then we can call _CrtDumpMemoryLeaks() to dump the memory leak. The memory leak will be shown in the Debugger window of the IDE (Visual Studio) with the line number of the memory allocation which has not been deallocated.

If you want the file name and the line number of the memory leak in your application, then overload the operator new macro with another macro which will use the file name and the line number.

C++
#define new new(_CLIENT_BLOCK,__FILE__, __LINE__)

By using the above code, the _CrtDumpMemoryLeaks function will show the file name and the line number of the memory leak in your application.

And if you want the memory leak information to be logged into a file, then use the following API with the required options.

_CrtSetReportMode

Function prototype:

C++
int _CrtSetReportMode(  int reportType, int reportMode );

Report type: _CRT_WARN, _CRT_ERROR, and _CRT_ASSERT

  • _CRT_WARN: Warning, messages, and information that do not need immediate attention
  • _CRT_ERROR: Errors, unrecoverable problems, and issues that require immediate attention
  • _CRT_ASSERT: Assertion failures

Report mode: _CRTDBG_MODE_DEBUG, _CRTDBG_MODE_FILE, _CRTDBG_MODE_WNDW, _CRTDBG_REPORT_MODE

  • _CRTDBG_MODE_DEBUG: Writes the message to the debugger's output window
  • _CRTDBG_MODE_FILE: Writes the message to a user-supplied file handle. _CrtSetReportFile should be called to define the specific file or stream to use as the destination.
  • _CRTDBG_MODE_WNDW: Creates a message box to display the message along with Abort, Retry, and Ignore buttons.
  • _CRTDBG_REPORT_MODE: Returns reportMode for the specified reportType: _CRTDBG_MODE_FILE, _CRTDBG_MODE_DEBUG, _CRTDBG_MODE_WNDW.

_CrtSetReportFile

After specifying _CRTDBG_MODE_FILE with _CrtSetReportMode, you can specify the file handle to receive the message text.

Function prototype:

C++
_HFILE _CrtSetReportFile(  int reportType, _HFILE reportFile );

So if you want to log a memory leak found in your application to a log file, then create a log file using the CreateFile Win32 API and then use _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE) and after that _CrtSetReportFile((_CRT_WARN, FileHandle). The file handle will be returned by the CreateFile Win32 API (which is used to create a file).

Sample Code

C++
int main()
{
    _CrtMemState &s1,&s2,&s3;
    // This line will take a snapshot
    // of the memory allocated at this point.
    _CrtMemCheckPoint(&s1);
    // your code
    // this line will take another snapshot
    // of the memory allocated at this point.
    _CrtMemCheckPoint(&s2);
    if (_CrtMemDifference(&s3,&s1,&s2))
    // _CrtMemDifference will calculate the memory allocation,
    // which has not been deallocated done between s1 and s2
    // memory check points.
    {
        _CrtDumpMemoryLeaks();
        // This line will dump the memory leak found between s1 and s2
        // memory check points.
    }
    return 0;
}

How Can We Avoid Getting Memory Leaks?

Make sure that:

  1. You correctly use 'new'/'delete' and 'new[]'/'delete[]' (also 'malloc'/'free').
  2. Pointers don't go out of scope before memory is released.
  3. If you allocate memory in a loop, you release it in each iteration.

Summary

Memory is a very important aspect to run any application, so avoid memory leaks while writing code by following the above guidelines, and get your code reviewed by other technical members of your team, or use the CrtDbg library APIs. The CrtDbg library works only in Debug mode, so you cannot use it to find memory leaks for Release mode applications.

Attached to this article is a sample application which uses CrtDbg APIs to find memory leaks. This sample application can be opened using Visual Studio 2005. Compile the application in Debug mode and execute it. It will show the memory leaks in the Debugger window of the Visual Studio 2005 IDE.

History

  • 17th March, 2010: Initial post
  • 19th March, 2010: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Kony, Hyderabad
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWrong report Pin
Shallyee Shang12-Feb-13 22:55
Shallyee Shang12-Feb-13 22:55 
QuestionMy mind Pin
Mast Avalons26-Jan-12 11:29
Mast Avalons26-Jan-12 11:29 
AnswerRe: My mind Pin
SuperMegaCoder30-Mar-12 10:42
SuperMegaCoder30-Mar-12 10:42 
GeneralRe: My mind Pin
naveenonnet19-Apr-12 20:06
naveenonnet19-Apr-12 20:06 
Questionfurther weakness Pin
Alain Weiler23-Nov-11 15:12
Alain Weiler23-Nov-11 15:12 
QuestionMisleading comment about _CrtDumpMemoryLeaks Pin
John Schroedl22-Aug-11 7:42
professionalJohn Schroedl22-Aug-11 7:42 
_CrtDumpMemoryLeaks();
 // This line will dump the memory leak found between s1 and s2
 // memory check points.


This comment is a bit misleading to me.

_CrtDumpMemoryLeaks shows memory not freed since the program began running and is not related to the memory checkpoints. _CrtMemDumpStatistics will show summary differences between checkpoints but not leaks.

I am hoping someone has a function to dump the differences/leaks between two checkpoints. Anyone?

John
AnswerRe: Misleading comment about _CrtDumpMemoryLeaks Pin
John Schroedl22-Aug-11 7:50
professionalJohn Schroedl22-Aug-11 7:50 
GeneralMy vote of 2 Pin
Niklas L20-Mar-10 22:47
Niklas L20-Mar-10 22:47 
GeneralEasier way to detect leaks [modified] Pin
Ivo Beltchev18-Mar-10 11:42
Ivo Beltchev18-Mar-10 11:42 
GeneralRe: Easier way to detect leaks Pin
naveenonnet18-Mar-10 18:06
naveenonnet18-Mar-10 18:06 
GeneralGood Pin
Ajay Vijayvargiya18-Mar-10 2:50
Ajay Vijayvargiya18-Mar-10 2:50 
GeneralExcellent Article on Memory management Pin
ramakoteswarrao.p17-Mar-10 20:33
ramakoteswarrao.p17-Mar-10 20:33 
GeneralGood Article on Memory Leak Detection Pin
sd_211117-Mar-10 20:20
sd_211117-Mar-10 20:20 
GeneralMy vote of 1 Pin
Niklas L17-Mar-10 12:08
Niklas L17-Mar-10 12:08 
GeneralRe: My vote of 1 Pin
naveenonnet17-Mar-10 19:57
naveenonnet17-Mar-10 19:57 
GeneralRe: My vote of 1 Pin
Niklas L18-Mar-10 1:54
Niklas L18-Mar-10 1:54 
GeneralRe: My vote of 1 Pin
naveenonnet18-Mar-10 2:55
naveenonnet18-Mar-10 2:55 
GeneralRe: My vote of 1 Pin
Niklas L18-Mar-10 4:38
Niklas L18-Mar-10 4:38 
GeneralRe: My vote of 1 Pin
naveenonnet18-Mar-10 18:04
naveenonnet18-Mar-10 18:04 
GeneralRe: My vote of 1 Pin
Niklas L18-Mar-10 23:00
Niklas L18-Mar-10 23:00 
GeneralRe: My vote of 1 Pin
ramakoteswarrao.p17-Mar-10 20:29
ramakoteswarrao.p17-Mar-10 20:29 
GeneralRe: My vote of 1 Pin
naveenonnet17-Mar-10 20:39
naveenonnet17-Mar-10 20:39 
GeneralMy vote of 1 Pin
Niklas L17-Mar-10 12:06
Niklas L17-Mar-10 12:06 
GeneralRe: My vote of 1 Pin
naveenonnet17-Mar-10 19:58
naveenonnet17-Mar-10 19:58 
GeneralExcellent! What a simple and effective way to detect memory leak Pin
Tage Lejon17-Mar-10 11:02
Tage Lejon17-Mar-10 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.