1*67e74705SXin Li================ 2*67e74705SXin LiLeakSanitizer 3*67e74705SXin Li================ 4*67e74705SXin Li 5*67e74705SXin Li.. contents:: 6*67e74705SXin Li :local: 7*67e74705SXin Li 8*67e74705SXin LiIntroduction 9*67e74705SXin Li============ 10*67e74705SXin Li 11*67e74705SXin LiLeakSanitizer is a run-time memory leak detector. It can be combined with 12*67e74705SXin Li:doc:`AddressSanitizer` to get both memory error and leak detection, or 13*67e74705SXin Liused in a stand-alone mode. LSan adds almost no performance overhead 14*67e74705SXin Liuntil the very end of the process, at which point there is an extra leak 15*67e74705SXin Lidetection phase. 16*67e74705SXin Li 17*67e74705SXin LiUsage 18*67e74705SXin Li===== 19*67e74705SXin Li 20*67e74705SXin LiLeakSanitizer is only supported on x86\_64 Linux. In order to use it, 21*67e74705SXin Lisimply build your program with :doc:`AddressSanitizer`: 22*67e74705SXin Li 23*67e74705SXin Li.. code-block:: console 24*67e74705SXin Li 25*67e74705SXin Li $ cat memory-leak.c 26*67e74705SXin Li #include <stdlib.h> 27*67e74705SXin Li void *p; 28*67e74705SXin Li int main() { 29*67e74705SXin Li p = malloc(7); 30*67e74705SXin Li p = 0; // The memory is leaked here. 31*67e74705SXin Li return 0; 32*67e74705SXin Li } 33*67e74705SXin Li % clang -fsanitize=address -g memory-leak.c ; ./a.out 34*67e74705SXin Li ==23646==ERROR: LeakSanitizer: detected memory leaks 35*67e74705SXin Li Direct leak of 7 byte(s) in 1 object(s) allocated from: 36*67e74705SXin Li #0 0x4af01b in __interceptor_malloc /projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3 37*67e74705SXin Li #1 0x4da26a in main memory-leak.c:4:7 38*67e74705SXin Li #2 0x7f076fd9cec4 in __libc_start_main libc-start.c:287 39*67e74705SXin Li SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s). 40*67e74705SXin Li 41*67e74705SXin LiTo use LeakSanitizer in stand-alone mode, link your program with 42*67e74705SXin Li``-fsanitize=leak`` flag. Make sure to use ``clang`` (not ``ld``) for the 43*67e74705SXin Lilink step, so that it would link in proper LeakSanitizer run-time library 44*67e74705SXin Liinto the final executable. 45*67e74705SXin Li 46*67e74705SXin LiMore Information 47*67e74705SXin Li================ 48*67e74705SXin Li 49*67e74705SXin Li`<https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer>`_ 50