For a large application, Memory Analysis usually generates an 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. Instead, the program can be manually instrumented with calls to the Memory Analysis tooling API to control parameters at runtime.
The Memory Analysis library supports extra commands and options that can be set using this API. To see their definitions, you must include rcheck/malloc.h in your source files. Also, the debug library must be preloaded for its specific option flags to have any effect.
#include <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; }
LD_PRELOAD=librcheck.so MALLOC_FILE=/tmp/trace.rmat \ MALLOC_TRACEBTDEPTH=10 MALLOC_START_TRACING=0 my_foo_app