Memory leak detection

If memory is allocated but not deallocated, then this eventually leads to memory exhaustion and abrupt termination.

Memory leaks are detected by using the Tools > Current Memory Usage menu item, which is available whenever Memory Debugging is enabled.

For each memory allocation, DDT records the stack trace and the requested allocation size. This allows it to know where in the code allocations occur, and how much is being used.

The Current Memory Usage dialog uses this information to plot the most commonly calling locations, in terms of bytes used.  If a leak is happening, it shows in this bar chart. Clicking through elements on the bar chart lists the pointers, and selecting a pointer shows the stack and the size of that allocation.

Custom Allocators and Class Constructors

Many codes often channel allocations through a small number of entry points.

For example, the constructor of a C++ class constructor would often be used to allocate memory. The most useful classes can be invoked throughout a program. In these cases, it is more helpful to group allocations by the line of code that called the constructor, so that the calls to the constructor are not lumped together in one unstructured form.

To group by calls to the constructor, right-click in a block in this bar chart to add that represented function as a Custom Allocator. Calls to that function are then grouped separately by call location.

Automating Leak Detection and Regression Testing

DDT has a non-interactive memory debugging mode which replicates the interactive information of the Current Memory Usage tool described above. This mode is often used during overnight tests or in continuous integration servers to measure memory usage and automatically ensure that leaks do not enter production code.

This mode creates an HTML file containing the memory allocations that remain after a process (or processes) terminates.

ddt --offline offline-log.html --mem-debug ... application.exe ....

This creates an annotated log file of the non-interactive debugging session which contains a leak report with the top leaks identified along with significant debugging events that are logged throughout the execution of the program.

Previous Next