-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Here you can find all information about the LeakSanitizer as well as the documentation of the available API.
Apart from finding memory leaks this sanitizer has a few more sophisticated features:
- The behaviour can be adjusted.
- Optional statistical bookkeeping can be queried with a C API.
- A few additional signal handlers.
The behaviour of this sanitizer can be adjusted using a few variables. They can be set using the C API or as environment variables.
All available variables can be found here.
Example:
LSAN_LEAK_COUNT=100 ./a.out
Example using the C API:
#include <lsan_internals.h> int main(void) { __lsan_leakCount = 100; }
The statistical bookkeeping can be activated and queried using the C API.
Activate the statistics using LSAN_STATS_ACTIVE
or __lsan_statsActive
as described above.
Read the documentation of the full set of functions here.
Example of printing the statistics:
#include <string.h> // For strdup(...) #include <lsan_internals.h> // For __lsan_statsActive #include <lsan_stats.h> int main(void) { __lsan_statsActive = true; void * pointer = strdup("Example leak"); __lsan_printStats(); __lsan_printFragmentationStats(); }
Signal | Action |
---|---|
SIGSEGV & SIGBUS
|
Caught and the stacktrace of the crash will be printed. |
SIGUSR1 |
Printing the current memory statistics. |
SIGUSR2 |
Printing a stacktrace where the signal was received. |
More about signal handlers here.
The LeakSanitizer can also help to find invalid de-allocations.
To do so enable LSAN_INVALID_FREE
and LSAN_STATS_ACTIVE
and deactivate
LSAN_INVALID_CRASH
(their documentation can be found here).
Example:
LSAN_INVALID_FREE=true LSAN_STATS_ACTIVE=true LSAN_INVALID_CRASH=false ./a.out
Note: This might lead to falsely detected invalid de-allocations.
If you chase an invalid de-allocation this might still be helpful, though.
Copyright (C) 2022 - 2024 mhahnFr.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".