[英]Valgrind reports 'possibly lost' memory when working with Boost threads
I have a program that runs some action in a separate therad, then joins on the thread, such as this one: 我有一个程序在一个单独的therad中运行一些动作,然后加入线程,比如这个:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void f() {
for (int i = 0; i < 100; ++i) cout << i << endl;
}
int main() {
boost::thread t(f);
t.join();
return 0;
}
If I run Valgrind on it, it reports 'possibly lost' memory. 如果我在其上运行Valgrind,它会报告“可能丢失”的内存。 This seems logical if I omit the
join()
, because in that case the thread is still running when the program exits. 如果省略
join()
,这似乎是合乎逻辑的,因为在这种情况下,当程序退出时线程仍在运行。 But if the thread is finished, I would expect that there are no warnings. 但如果线程完成,我希望没有警告。
Here is the backtrace: 这是回溯:
==8797== 288 bytes in 1 blocks are possibly lost in loss record 2 of 3
==8797== at 0x4A1F8B3: calloc (vg_replace_malloc.c:467)
==8797== by 0x400F289: allocate_dtv (in /lib64/ld-2.4.so)
==8797== by 0x400F34D: _dl_allocate_tls (in /lib64/ld-2.4.so)
==8797== by 0x53EF981: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.4.so)
==8797== by 0x4B3311D: boost::thread::start_thread() (in /home/egbomrt/BOOST/inst_1_47_0/lib/libboost_thread.so.1.47.0)
==8797== by 0x40A20C: boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type) (thread.hpp:204)
==8797== by 0x406295: main (main.cpp:12)
Is this a problem with Boost Thread, Posix Thread or is this perfectly normal? 这是Boost Thread,Posix Thread的问题还是这个完全正常? I could just create a suppression rule for it, but it would also be good if I got a warning if there is an unfinished thread, but not when all threads are finished.
我可以为它创建一个抑制规则,但如果我有一个未完成的线程,但是当所有线程都完成时,我会收到警告。
I found that the problem is with the pthread library. 我发现问题出在pthread库中。 If I run the program on SUSE 10, I get the memory leaks, but if I run it on SUSE 11, I don't get the problem.
如果我在SUSE 10上运行该程序,我会得到内存泄漏,但是如果我在SUSE 11上运行它,我就不会遇到问题。
I get the same results with and without Boost. 无论有没有Boost,我都会得到相同的结果。
Thanks for the comments. 感谢您的评论。 That helped me pinpoint the problem.
这帮我找出了问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.