Channels ▼

Simple Memory Leak Detection

February 2002/Simple Memory Leak Detection

Memory leaks are a problem that any developer may face at some time, and leaks have their own mysterious ways of showing up. The most common is Dr Watson errors on Windows and core dumps on Unix systems. A message such as “Access Violation” or “Segmentation Violation” happens when a pointer tries to access a memory location outside the process’ data segment. The operating system raises the red flag and crashes the process.

Under ideal conditions, a pointer should not be accessing memory locations outside the address range of data segment. However, the programmer is only human and C/C++ pointers have humbled many. We forget to deallocate memory or use the pointer even after it has been deleted or gone out of scope or leave a dangling pointer. The pointer, now, has a junk value. Sometimes, the pointer has a valid address but sometimes, fortunately or unfortunately (depending on which side you are), the address is outside the data segment and the program crashes and the problem is identified only after hours of re-reading the code.

The easiest solution to find this problem is to use software like Parasoft’s Insure++ or CompuWare’s BoundsChecker. But you can chase down memory leaks even if you’re not using one of these tools. The simplest way to find a memory leak is with the Windows Task Manager. Run the application and monitor Memory Usage and Virtual Memory Size in the Process tab or use the Windows Resource kit. A continuous increase in virtual memory, after the application has allocated all the memory it needs, indicates a memory leak. If it’s a very small and subtle memory leak, it would take some hours to identify, but its well worth the effort. The Process Viewer (PVIEWER.EXE) in Windows Resource Kit provides a lot of details about the process, but it needs to be refreshed to update the status. The advantage with the Task Manager is that it updates automatically.

Now that we know that the program has a memory leak, we need to find it in order to fix it. My favorite tool is the Visual C++ Debug version of the C Runtime Library. It provides several functions to dump heap snapshots and track down memory leaks. The best part is that these functions are removed during preprocessing, if _DEBUG is not defined. So, just change the Build configuration from Debug to Release and the executable does not have these functions.

There are many useful functions in the Debug Library, but I am going to list the most important and the ones required to identify a memory leak. Example 1 shows a small implementation of the Debug functions.

This program has a very obvious memory leak. I have used three debug functions here, which are the minimum required to determine a memory leak. _CrtSetReportMode() and _CrtSetReportFile() are used to define the destination of the warnings, errors, and assertions. In Example 1, the report mode is set to write to a file. The report file is then set to send the output to stdout. _CrtDumpMemoryLeaks() determines if a memory leak has occurred since the start of program execution. It dumps all information for a block that is not deallocated. Example 1 results in the output in Example 2.

The output shows that I missed deallocating a 100 byte block and the data in that block. Since this was a very small program, it was very easy to identify the memory leak. But, in thousands of lines of code, it’s not going to be so easy. There are more Debug functions that would make the task easier.

Besides forgetting to deallocate memory, another problem is writing beyond the allocated memory. This causes memory corruption. And the Debug version of C Runtime library has just the function we need: _CrtCheckMemory(). This function validates the integrity of the memory blocks allocated by the debug heap manager. Consider this example:

char *ptr2;
ptr2 = new char[10];

// copying 12 char in string for 10

The resulting output of this code would be:

memory check error at
    0x00431CBA = 0x4B, should be 0xFD.
memory check error at
    0x00431CBB = 0x4C, should be 0xFD.
memory check error at
    0x00431CBC = 0x00, should be 0xFD.
DAMAGE: after Normal block
    (#43) at 0x00431CB0.
Normal located at
    0x00431CB0 is 10 bytes long.

Other functions which I have found useful are _CrtMemCheckpoint(), _CrtMemDifference(), and _CrtMemDumpAllObectsSince(). _CrtMemCheckpoint() generates a snapshot of the state of the debug heap. This function has maximum use when the snapshot is taken at two different places in the program execution. The difference, using _CrtMemDifference(), will tell if there was a difference in heap usage at the two locations. This helps to identify the guilty section of the code:

_CrtMemState sh1, sh2, sh_diff;
char *ptr1 = new char[100];
char *ptr2 = new char[10];
_CrtMemDifference(&sh_diff, &sh1, &sh2);

_CrtMemDumpAllObjectsSince() is used by _CrtDumpMemoryLeaks() to determine if there was a memory leak. _CrtMemDumpAll-ObjectsSince() displays the debug heap from the start of program execution if NULL is passed or the status of the heap from the checkpoint or last heap snapshot if the checkpoint is passed.

Listing 1 shows the three examples with comments. Using the Task Manager and Process Viewer in conjunction with the Debug Version of C Runtime library will reduce a lot of pain and frustration caused by memory leaks and memory corruption.

Sumit Agarwal has been working on C++ on UNIX and Windows for the past seven years and presently works for HCL Perot Systems just outside Philadelphia. He can be reached at [email protected].

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.