Memory Leak Tracing in Linux Using mtrace

C, C++, Unix No Comments »

Today I ran across an article on DevX.com entitled Identifying Memory Leaks in Linux for C++ Programs. This article describes a utility called mtrace which, in concert with the mtrace() function in the GNU libc, allows one to easily identify memory leaks in C programs.

To use mtrace, follow these steps:

  • Set the environment variable MALLOC_TRACE to point to a file where mtrace will log memory allocations.
  • Insert a call to mtrace() within your code before any memory is allocated.
  • Compile the program with debugging options set (GCC’s -g flag)
  • Execute the program.
  • Use mtrace(1) on the trace log file to view the memory leaks.

I shall have to look at the GNU libc source code to see how mtrace is implemented. I have a few educated guesses, but I can’t seem to piece together the whole picture.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in