For a large application, memory analysis usually generates and excessive amount of data that's often hard to comprehend. One method of dealing with this data is to use runtime control options for the application; however, that might not always be feasible. In this case, the program can be manually instrumented with calls to memory analysis tooling to control parameters at runtime.
The Memory Analysis API lets you:
The Memory Analysis library supports extra options that can be set using this API. To include definitions of extra commands, use #include <rcheck/malloc.h>; otherwise, you can use numeric constants. If the debug library isn't preloaded, its specific option flags won't have any effect.
The following example shows how to use the API tool to collect any allocation from a specific function call, and then check for leaks afterward:
#include <malloc/malloc.h> #include <rcheck/malloc.h> void bar() { char * p = malloc(30); // irrelevant malloc free(p); } char * foo() { char * p = malloc(20); // relevant malloc return p; } int main(){ bar(); mallopt(MALLOC_TRACING,1); // start tracing foo(); mallopt(MALLOC_TRACING,0); // stop tracing mallopt(MALLOC_DUMP_LEAKS, 1); // dump memory leaks return 0; }
To run the example application above, you'd use the command such as:
LD_PRELOAD=librcheck.so MALLOC_FILE=/tmp/trace.rmat \ MALLOC_TRACEBTDEPTH=10 MALLOC_START_TRACING=0 my_foo_app
Then, you can load the resulting trace file into IDE. The result should report the following: