Tooling API

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 API lets you:
Note: There is only one API function that can be used: mallopt(). See the mallopt() entry in the C Library Reference for details on all commands and options related to Memory Analysis.

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.

The following example shows how to use the API to collect allocation details from a specific function call and check for leaks afterwards:
#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;
}
To run the sample application above, you would use a command such as:
LD_PRELOAD=librcheck.so MALLOC_FILE=/tmp/trace.rmat \
MALLOC_TRACEBTDEPTH=10 MALLOC_START_TRACING=0 my_foo_app
Then, you would load the resulting trace file into the IDE. The result should report the following: