【问题标题】:Valgrind reports 'possibly lost' memory when working with Boost threadsValgrind 报告在使用 Boost 线程时“可能丢失”内存
【发布时间】:2012-01-01 07:50:00
【问题描述】:

我有一个程序在单独的线程中运行一些操作,然后加入线程,例如这个:

#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;
}

如果我在其上运行 Valgrind,它会报告“可能丢失”内存。如果我省略join(),这似乎是合乎逻辑的,因为在这种情况下,当程序退出时线程仍在运行。但如果线程结束,我希望没有警告。

这是回溯:

==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)

这是 Boost Thread、Posix Thread 的问题还是完全正常?我可以为它创建一个抑制规则,但是如果有一个未完成的线程,但不是在所有线程都完成时,我得到一个警告,那也很好。

【问题讨论】:

  • 您可以自己消除 phtread(只需使用 pthread_create/pthread_join 而不使用 boost)
  • 无法在 GCC4.6.2/Boost 1.48 上重现。但是,我确实在 1 个块中获得了 8 个字节“仍然可以访问”。顺便说一句,它与 &lt;thread&gt; 而不是 Boost 一起工作得很好。
  • @sehe:从堆栈跟踪来看,它与 boost 无关。它是 pthread_create() 为 TLS 分配结构。见akkadia.org/drepper/tls.pdf
  • 我正在使用 gcc 4.1.2 和 Boost 1.47。
  • 与 gcc 7.2 和 boost 1.65 相同的问题/报告

标签: c++ multithreading g++ valgrind boost-thread


【解决方案1】:

我发现问题出在 pthread 库上。如果我在 SUSE 10 上运行该程序,我会遇到内存泄漏,但如果我在 SUSE 11 上运行它,我不会遇到问题。

无论有没有 Boost,我都得到了相同的结果。

感谢 cmets。这有助于我查明问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多