2011-06-12 191 views
17

有人能让我知道boost线程库是否泄漏。在我看来,它确实: 谷歌说我应该使用boost线程和pthread来编译我正在做的,而在版本1.40中这个问题已经解决了,但是我仍然有漏洞。请注意,这将编译得很好,但会检测到泄漏。Boost线程泄漏C++

#include <boost/thread.hpp> 
#include <boost/date_time.hpp> 

void t1(){} 

int main(void){ 
boost::thread th1(t1); 
th1.join(); 
return 1; 
} 

随着Valgrind的我得到以下输出

HEAP SUMMARY: 
==8209==  in use at exit: 8 bytes in 1 blocks 
==8209== total heap usage: 5 allocs, 4 frees, 388 bytes allocated 
==8209== 
==8209== 8 bytes in 1 blocks are still reachable in loss record 1 of 1 
==8209== at 0x4024F20: malloc (vg_replace_malloc.c:236) 
==8209== by 0x4038CCB: boost::detail::get_once_per_thread_epoch() (in /usr/local/lib/libboost_thread.so.1.42.0) 
==8209== by 0x40329D4: ??? (in /usr/local/lib/libboost_thread.so.1.42.0) 
==8209== by 0x4032B26: boost::detail::get_current_thread_data() (in /usr/local/lib/libboost_thread.so.1.42.0) 
==8209== by 0x4033F32: boost::thread::join() (in /usr/local/lib/libboost_thread.so.1.42.0) 
==8209== by 0x804E7C3: main (testboost.cpp) 
==8209== 
==8209== LEAK SUMMARY: 
==8209== definitely lost: 0 bytes in 0 blocks 
==8209== indirectly lost: 0 bytes in 0 blocks 
==8209==  possibly lost: 0 bytes in 0 blocks 
==8209== still reachable: 8 bytes in 1 blocks 
==8209==   suppressed: 0 bytes in 0 blocks 

我也试图与在以下网站上列出的代码:http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html 还是同样的问题。

+0

看看boost源文件中的src/pthread/Once.cpp。很明显,它不会泄漏(只需查找它使用的pthread库函数的定义)。 – Mankarse 2011-06-12 11:23:22

+0

使用'std :: thread'而不是boost,代码甚至不会运行;它终止于一个'std :: system_error'异常。 – 2011-06-12 11:41:04

+0

只是尝试在上面的链接代码,你最有可能会看到valgrind – 2011-06-12 18:32:11

回答

10

这与升级1_46_1有关,因此它可能不适用于您正在使用的版本。如果你真的想说服自己,请看助推源。 (当我运行您的示例代码时,OSX上的泄漏检测器未检测到任何泄漏)。

这不是一个实际的泄漏(除非pthreads,您使用的过时版本的boost或您的编译器存在缺陷)。

get_once_per_thread_epoch mallocs一个新的uintmax_t并映射到线程本地存储与epoch_tss_key有一个相关的析构函数释放映射的数据。因此,malloced内存保证被释放。

我真的不明白为什么valgrind会将此检测为泄漏,但可能是因为pthread退出函数在valgrind函数之后的某个点执行。另一种可能是pthread函数本身在泄漏,但我没有在文档中看到任何暗示这种情况的东西。

+0

谢谢。我升级到1.46.1和valgrind发生同样的错误。直接使用pthread编码不会导致此错误。必须像你说的那样,Valgrind可以检测到线程的清洁 – 2011-06-12 18:33:46