Skip to content
mhahnFr edited this page Jul 4, 2025 · 17 revisions

Here you can find all information about the LeakSanitizer as well as the documentation of the available API.

The features

Apart from finding memory leaks this sanitizer has a few more sophisticated features:

  • Crashes are caught.
  • The behaviour can be adjusted.
  • Optional statistical bookkeeping can be queried with a C API.

Behaviour

The behaviour of this sanitizer can be adjusted using environment variables.

Tip

Example:

LSAN_INDIRECT_LEAKS=true ./a.out

Statistics

The statistics can be printed automatically by setting the environment variable LSAN_AUTO_STATS to a valid time interval.

Alternatively, the C API can be used. Activate the statistical bookkeeping by setting the environment variable LSAN_STATS_ACTIVE to true.

Read the documentation of the full set of functions here.

Tip

Example of printing the statistics:

#include <string.h> // For strdup(...)

#include <lsan_stats.h>

int main(void) {
    void* pointer = strdup("Example leak");

    __lsan_printStats();
    __lsan_printFragmentationStats();
}

Execute it as follows:

LSAN_STATS_ACTIVE=true ./a.out

Signal handlers

The following signal handlers are available:

Signal Action
SIGUSR1 Prints the current memory statistics if they have been activated.
SIGUSR2 Prints a stacktrace where the signal was received.
Any deadly signal is caught and a stacktrace of the crash is printed.

More about signal handlers here.

Clone this wiki locally