如何使LeakSanitizer忽略程序泄漏的结尾 [英] How do I make LeakSanitizer ignore end of program leaks

查看:962
本文介绍了如何使LeakSanitizer忽略程序泄漏的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LeakSanitizer来检测泄漏的内存,但是我正在使用的程序样式无法在exit之前释放内存.以我的经验,这是相当普遍的.

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 experience.

我想检测到此泄漏:

int main(int argc, char const *argv[])
{
    char *p = malloc(5);
    p = 0;
    return 0;
}

并忽略此泄漏:

int main(int argc, char const *argv[])
{
    char *p = malloc(5);
    return 0;
}

推荐答案

您希望LSan仅报告不可达的泄漏,即保证程序泄漏的指针.问题在于,默认情况下,LeakSanitizer会在程序结束时运行检查,通常是在全局C ++ dtor已完成并且它们的内容不再被视为可访问之后.因此,当LSan最终运行时,必须假设不再有很多东西可用.要变通解决此问题,您可以添加

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

从主站点返回之前(受问题719

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

PS:请谨慎使用非常简单的示例,例如您上面发布的示例.即使在-O0,GCC也会经常删除未使用的分配和分配,因此请始终检查汇编器是否符合您的期望.

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.

这篇关于如何使LeakSanitizer忽略程序泄漏的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆