How do I make LeakSanitizer ignore end of program leaks

后端 未结 1 669
误落风尘
误落风尘 2021-02-15 23:38

I want to use LeakSanitizer to detect leaked memory, but the style of the program I am using does not free memory before exit. This is fairly common in my experienc

相关标签:
1条回答
  • 2021-02-16 00:22

    You want LSan to report only unreachable leaks i.e. pointers which are guaranteed to be leaked by the program. Problem is that by default LeakSanitizer runs it's checks at the end of the program, often after global C++ dtors have completed and their contents is no longer considered accessible. So when LSan finally runs, it has to assume that lots of stuff is no longer reachable. To work around this issue you can add

    #include <lsan_interface.h>
    ...
    #ifdef __SANITIZE_ADDRESS__
      __lsan_do_leak_check();
      __lsan_disable();
    #endif
    

    before returning from main (inspired by Issue 719 and llvm discussion).

    PS: Be careful with extremely simple examples like the ones you post above. GCC will often remove unused assignments and allocations even at -O0 so always check that assembler matches your expectations.

    0 讨论(0)
提交回复
热议问题