LeakSanitizer:获取运行时泄漏报告?

rmc*_*701 4 c++ linux multithreading memory-leaks address-sanitizer

我继承了一些旧代码,似乎在某处内存泄漏。我的本能是

-faddress=sanitize -fno-omit-frame-pointer
Run Code Online (Sandbox Code Playgroud)

然后让Address Sanitizer的工具系列为我找到泄漏点。但是,我感到非常失望。我希望收到某种运行时错误消息(类似于您不应读写的地址清理程序的错误)。直到程序成功完成之后,泄漏清理器才似乎不进行任何泄漏检查分析。我的问题是,我继承的代码具有多个线程,而并非旨在将所有线程都加入以准备软着陆。

我用一个简单的例子简化了我的问题:

#include <thread>                                           
#include <chrono>                                           
#include <iostream>                                         

bool exit_thread = false;                                   

void threadFunc()                                           
{                                                           
   while(!exit_thread)                                      
   {                                                        
      char* leak = new char[256];                           
      std::this_thread::sleep_for(std::chrono::seconds{1}); 
   }                                                        
}                                                           

int main() {                                                
   std::thread t(threadFunc);                               
   std::cout << "Waiting\n";                                
   std::this_thread::sleep_for(std::chrono::seconds{5});    
   exit_thread = true;                                      
   std::cout << "Exiting\n";                                
   //Without joining here I do not get the leak report.     
   t.join();                                                
   return 0;                                                
}    
Run Code Online (Sandbox Code Playgroud)

我用这个编译

clang++ leaker.cpp -fsanitize=address -fno-omit-frame-pointer -g -O0 -std=c++1y -o leaker      
Run Code Online (Sandbox Code Playgroud)

然后跑

ASAN_OPTIONS='detect_leaks=1' LSAN_OPTIONS='exitcode=55:report_objects=true:log_threads=true:log_pointers=true' ./leaker                                                
Run Code Online (Sandbox Code Playgroud)

(我在这里对“ LSAN_OPTIONS”有点疯狂,因为我在玩耍……没有一个选项可以满足我的要求,但是一旦得知泄漏就退出了)。

如代码中所述,如果我加入线程然后退出程序,则会得到一个漂亮的泄漏报告。否则我什么也得不到。正如你可以成像追踪到遗留代码库10-100线程,并把他们都下去好听点是笨重。

几年前,我记得玩过Visual Leak Detector,并祝好运,因为它会生成报告,报告包含所有潜在的内存泄漏(而且我不记得必须将所有内容都拆下来了)。问题是此工具仅适用于Windows,而我的代码仅适用于Linux。我可以使LeakSanitizer工具做类似的事情吗?

Lek*_*eyn 6

LeakSanitizer的公共接口(sanitizer / lsan_interface.h)具有各种功能,可以满足您的需求。函数__lsan_do_leak_check()执行检查并在发现泄漏时终止。还有__lsan_do_recoverable_leak_check一个不会终止,可以多次调用。

考虑对程序的此修改:

#include <thread>
#include <chrono>
#include <iostream>
#include <sanitizer/lsan_interface.h>

bool exit_thread = false;

void threadFunc()
{
   while(!exit_thread)
   {
      char* leak = new char[256];
      std::this_thread::sleep_for(std::chrono::seconds{1});
   }
}

int main() {
   std::thread t(threadFunc);
   std::cout << "Waiting\n";
   std::this_thread::sleep_for(std::chrono::seconds{5});
   exit_thread = true;
   std::cout << "Exiting\n";
   //Without joining here I do not get the leak report.
   //t.join();
   __lsan_do_recoverable_leak_check();
   std::cout << "Done\n";
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

Waiting
Exiting

=================================================================
==29240==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1024 byte(s) in 4 object(s) allocated from:
    #0 0x4d9a30 in operator new[](unsigned long) (leaker+0x4d9a30)
    #1 0x4dc663 in threadFunc() leaker.cpp:12:20
    #2 0x4dffe3 in void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1530:18
    #3 0x4dff94 in std::_Bind_simple<void (*())()>::operator()() /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1520:16
    #4 0x4dfcc8 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/thread:115:13
    #5 0x7f0a9664034f in execute_native_thread_routine /build/gcc-multilib/src/gcc-5.2.0/libstdc++-v3/src/c++11/thread.cc:84

SUMMARY: AddressSanitizer: 1024 byte(s) leaked in 4 allocation(s).
Done
terminate called without an active exception
Aborted
Run Code Online (Sandbox Code Playgroud)