Finding memory leaks

Many IDE tools track individual heap blocks so they can detect and report memory leaks. The reported details often include stack traces of allocation points, so you can identify pointers related to leaks.

The tools used for finding leaks are the same ones used for finding memory corruption, but these two activities require distinct ways of using the tools. Often, there's a trade-off between the amount and type of leaks that a tool can find versus its setup time and overhead. More leak details typically mean more setup time and greater overhead. In general, you should try using the tools in the following order:
  1. System Information

    In the QNX System Information perspective, you can learn which processes are using the most heap memory through the System Resources view. Then, you can examine the heap usage for a particular process through the Malloc Information view. The information shown here lets you quickly spot when the process is likely leaking memory.

    This is the best tool to use first because you can switch to this perspective at any time to see the statistics. You don't need to reconfigure, recompile, or relaunch your application, and there's no application overhead because the statistics come from the general-purpose process-level allocator.

  2. Memory Analysis

    Memory Analysis displays memory trace data generated by the debug allocation library (librcheck). These data describe all allocations and deallocations, and any blocks that get leaked.

    This tool has an easy setup, doesn't require recompiling the application, and imposes relatively low overhead. One drawback is that if the application crashes, librcheck might not report some of the current memory leaks because memory tracing might stop working at that point.

  3. Valgrind Memcheck

    This tool intercepts all calls to malloc(), free(), new, and delete to track dynamic-memory blocks. By examining the allocation stack traces and the pointer values in a program, Memcheck can detect different kinds of leaks, such as possibly or indirectly lost blocks.

    Like Memory Analysis, Memcheck doesn't require recompiling the application. However, it imposes noticeably more memory and CPU overhead, and checks for leaks only when the application exits.

  4. Valgrind Massif

    This tool measures heap usage and tells you which areas of a program use the most heap memory. Massif finds blocks that aren't accessed anymore but aren't freed and thus, waste space.

    Like Memcheck, Massif significantly slows the program and reports the results only at exit time. But it is useful when you want to further reduce memory usage to improve performance.

You must run a binary built with debug information so the active tool can match binary instructions with lines of code and display the symbols (i.e., backtrace) in the results. To see symbols for shared libraries, your host machine must store debug versions of these libraries. Any binary with debug information has a bug icon (Icon: Small blue bug) next to its name in the Project Explorer. The binary selected for running when you launch a project depends on the launch configuration settings.