使用thread_local时在gcc 4.8.1中发生内存泄漏? [英] Memory leak in gcc 4.8.1 when using thread_local?

查看:336
本文介绍了使用thread_local时在gcc 4.8.1中发生内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Valgrind通过以下代码报告泄漏的块,显然每个线程一个:

Valgrind is reporting leaked blocks, apparently one per thread, in the following code:

#include <iostream>
#include <thread>
#include <mutex>
#include <list>
#include <chrono>

std::mutex cout_mutex;

struct Foo
{
    Foo() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }

    ~Foo() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }

    void 
    hello_world() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }
};

void
hello_world_thread()
{
    thread_local Foo foo;

    // must access, or the thread local variable may not be instantiated
    foo.hello_world();

    // keep the thread around momentarily
    std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
}

int main()
{
    for ( int i = 0; i < 100; ++i )
    {
        std::list<std::thread> threads;

        for ( int j = 0; j < 10; ++j )
        {
            std::thread thread( hello_world_thread );
            threads.push_back( std::move( thread ) );
        }

        while ( ! threads.empty() )
        {
            threads.front().join();
            threads.pop_front();
        }
    }
}

编译器版本:

$ g++ --version
g++ (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

GCC构建选项:

--enable-shared
--enable-threads=posix
--enable-__cxa_atexit
--enable-clocale=gnu
--enable-cxx-flags='-fno-omit-frame-pointer -g3'
--enable-languages=c,c++
--enable-libstdcxx-time=rt
--enable-checking=release
--enable-build-with-cxx
--disable-werror
--disable-multilib
--disable-bootstrap
--with-system-zlib

程序编译选项:

g++ -std=gnu++11 -Og -g3 -Wall -Wextra -fno-omit-frame-pointer thread_local.cc

valgrind版本:

valgrind version:

$ valgrind --version
valgrind-3.8.1

Valgrind选项:

Valgrind options:

valgrind --leak-check=full --verbose ./a.out > /dev/null

valgrind输出的尾端:

Tail-end of valgrind output:

==1786== HEAP SUMMARY:
==1786==     in use at exit: 24,000 bytes in 1,000 blocks
==1786==   total heap usage: 3,604 allocs, 2,604 frees, 287,616 bytes allocated
==1786== 
==1786== Searching for pointers to 1,000 not-freed blocks
==1786== Checked 215,720 bytes
==1786== 
==1786== 24,000 bytes in 1,000 blocks are definitely lost in loss record 1 of 1
==1786==    at 0x4C29969: operator new(unsigned long, std::nothrow_t const&) (vg_replace_malloc.c:329)
==1786==    by 0x4E8E53E: __cxa_thread_atexit (atexit_thread.cc:119)
==1786==    by 0x401036: hello_world_thread() (thread_local.cc:34)
==1786==    by 0x401416: std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (functional:1732)
==1786==    by 0x4EE4830: execute_native_thread_routine (thread.cc:84)
==1786==    by 0x5A10E99: start_thread (pthread_create.c:308)
==1786==    by 0x573DCCC: clone (clone.S:112)
==1786== 
==1786== LEAK SUMMARY:
==1786==    definitely lost: 24,000 bytes in 1,000 blocks
==1786==    indirectly lost: 0 bytes in 0 blocks
==1786==      possibly lost: 0 bytes in 0 blocks
==1786==    still reachable: 0 bytes in 0 blocks
==1786==         suppressed: 0 bytes in 0 blocks
==1786== 
==1786== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--1786-- 
--1786-- used_suppression:      2 dl-hack3-cond-1
==1786== 
==1786== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

构造函数和析构函数对每个线程运行一次:

Constructors and destructors were run once for each thread:

$ ./a.out | grep 'Foo::Foo' | wc -l
1000

$ ./a.out | grep hello_world | wc -l
1000

$ ./a.out | grep 'Foo::~Foo' | wc -l
1000

注意:

  • 如果更改创建的线程数,则泄漏的块数与线程数匹配.
  • 代码的结构使得可能可以在实现GCC的情况下允许资源重用(即泄漏的块).
  • 在valgrind堆栈跟踪中,thread_local.cc:34是以下行:thread_local Foo foo;
  • 由于调用sleep_for(),程序运行大约需要10秒钟左右.
  • If you change the number of threads created, the number of leaked blocks matches the number of threads.
  • The code is structured in such a way that might permit resource reuse (i.e. the leaked block) if GCC were so implemented.
  • From the valgrind stacktrace, thread_local.cc:34 is the line: thread_local Foo foo;
  • Due to the sleep_for() call, a program run takes about 10 seconds or so.

您是否知道此内存泄漏是由于我的配置选项导致的GCC还是程序中的某些错误?

Any idea if this memory leak is in GCC, a result of my config options, or is some bug in my program?

推荐答案

似乎泄漏来自动态初始化.

以下是int的示例:

thread_local int num=4; //static initialization

最后一个示例不泄漏.我尝试了2个线程,完全没有泄漏.

The last example does not leak. I tried it with 2 threads and no leak at all.

但是现在:

int func()
{
    return 4;
}
thread_local int num2=func(); //dynamic initialization

这一个泄漏!有2个线程,它给出total heap uage: 8 allocs, 6 frees, 428 bytes allocated ...

This one leak ! With 2 threads it gives total heap uage: 8 allocs, 6 frees, 428 bytes allocated...

我建议您使用类似的解决方法:

I would suggest you to use a workaround like :

thread_local Foo *foo = new Foo; //dynamic initialization

别忘了在线程执行结束时去做:

No forget at the end of the thread execution to do :

delete foo;

但是最后一个例子是一个问题:如果线程在删除之前退出并出现错误怎么办?再次泄漏...

But the last example as one problem : What if the thread exit with error before your delete ? Leak again...

似乎没有很好的解决方案.也许我们应该向g++开发人员报告有关情况?

It seems that there is no great solution. Maybe we should report that to the g++ developers about that ?

这篇关于使用thread_local时在gcc 4.8.1中发生内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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