Software Debugging - Memory Leaks

This page contains information and links related to software debugging, especially focused on memory leaks.

Software Debugging and Debuggers

Debugging Microsoft Windows Programs

Some useful links with information and tools for debugging Windows programs:

For finding errors that occur in release versions at customer sites the following utilities are useful:

Source Code Checking (Static Error Checking)

For related information and products see the Software Quality page.

Memory Leak Debugging (Dynamic Runtime Error Checking)

[For related information and products see the Software Quality page.]

Memory management in C and C++ is and has ever been a great source of problems (not only for C/C++ beginners) - and debugging memory errors can be a nightmare. Allocating memory on the heap (with malloc / calloc / alloca / realloc / xmalloc / xcalloc / xrealloc / strdup (!) / new / new[], ...), accessing memory with pointers and, probably the biggest problem for many developers, freeing memory (with free / cfree / xfree / delete / delete[], ...) at the right place and time must be done very carefully. Memory errors are often very strange and are not easy to detect, because the reason for them and the location they occur are usually not directly related. Errors like

  1. invalid references to free memory: reading and writing to free memory with uninitialized or already freed pointers, double freeing of the same memory, reallocating previously freed memory
  2. memory overwrites: writing before (buffer underflow) and beyond (buffer overflow) an allocated memory (or stack) area
  3. retaining pointers to reallocated memory: due to a different address for the reallocated memory, all related pointers have to be actualized
  4. reading from uninitialized allocated memory: besides the allocation with calloc(), allocated memory is not initialized, reading such memory will produce non-deterministic behaviour
  5. memory leaks: failure to free no longer used memory by constantly allocating new memory and assigning it to the same pointer or by leaving the scope where the pointer is valid (automatic pointer variable on the stack) and thus having no chance to free the memory later increases the memory usage
are well known problems to every C and C++ programmer. C++ introduced another quality of problems with the underlying allocation / deallocation of memory and its related object constructors and destructors, not spoken of such specials like objects destroying themselves ("delete this;"), static objects with their constructors (called before entry to main()) and destructors (called after exit of main()) or mixed C and C++ programming with an erratic mix of malloc / delete and new / free.

For applications in the commercial / industrial area which have to run for weeks or months without interruption and shutdown, leaking memory (as well as other system resources) is probably one of the greatest problems: After some time memory leaking decreases the system performance and leads to non-deterministic errors and malfunctions where the reason for this behaviour is usually not obvious.

For the given reasons, various debugging methods and techniques have been proposed, many papers have been published and a lot of different debugging extensions for the Standard C/C++ memory handling as well as specialized debugging tools have been developed in the past to overcome the described problems. The following list contains informational links as well as links to free and commercial debugging libraries and tools:

Information:

Commercial Libraries/Tools: Free Libraries/Tools:

Very useful, complete and easy to use memory debugging tools:

Other free memory debugging tools (details unknown or untested):

There are also a lot of theoretical papers, white papers and product manuals about memory management, memory debugging and memory error detection available on the Web as well as in printed magazines. For example, over the past years, I have found many interesting articles in Dr. Dobb's Journal (DDJ): "Stalking the Wild Memory Allocator" (C-Chest Column, DDJ 6/1988),  "C Dynamic Memory Use" (DDJ 8/1989), "Encapsulating C Memory Allocation" (DDJ 8/1990), "Debugging Memory Allocation Errors" (DDJ 8/1990), "Rethinking Memory Management" (DDJ 6/1994), "A Memory Controller" (DDJ 5/1990), "The Mtlib Memory-Tracking Library" (DDJ 10/2003). There was also an interesting white paper available from MicroQuill about HeapAgent with very good basics and background information. ParaSoft also provides white papers for Insure++ and its other tools.

Here are some articles and research papers about debugging techniques:

Garbage Collection

To overcome the problems of erratic access to allocated memory regions and leaking memory, the so called Garbage Collection (GC) has been invented. For some programming languages like LISP or Java, but not C and C++, garbage collection is part of the program runtime system / ennvironment. For example, Java has a "new" operator to allocate memory, but no "delete" operator like C++, because the unused memory will be detected and released by the Java Garbage Collector. In fact, Java does not have pointers, it works only with references. [But even with references and garbage collection, Java can produce memory leaks - see "Java Q&A: How Do You Plug Java Memory Leaks?", Dr. Dobbs Journal 2/2000]

Garbage Collectors use several strategies to find no longer used memory regions. They usually run in the background as a low priority process / thread or are directly invoked from the source at distinct locations. However, this introduces an unpredictable behaviour that leads sometimes to problems where the program execution is very time sensitive as it is in real-time systems, e.g. embedded controls, where a strict deterministic behaviour must be guaranteed. That is the reason why Java cannot be used in such systems without a complete re-design of its underlying garbage collection mechanism, as it has been done from several companies.

The following list contains some links to free and commercial garbage collection packages as well as general information about garbage collection:

Strack Trace

Tools for Stack Tracing:

Buffer Overflow

Closely related to memory problems is the so called buffer overflow that can lead to security problems. Following are links with information about and tools that deal with this problem:

Journals related to Software Debugging

See the Software Journals list.

[Homepage] [Haftungsausschluss]
This page has been accessed  times since December 31st, 1999.
Copyright © 1999-2008 Juergen Mueller [EMail]. Last update: April 28th, 2008.