win32 std :: thread是否会泄漏内存?

时间:2014-02-24 11:43:35

标签: c++ multithreading c++11 memory-leaks

我使用Windows 7 32位下的MinGW gcc 4.8.2编译了以下代码:

// t.cpp
#include <iostream>
#include <thread>

class Mine
{
public:
    Mine() { std::cout << "Ctor called\n"; }
    ~Mine() { std::cout << "Dtor called\n"; }
};

void foo(const Mine &a, const Mine &s)
{
}

int main()
{
    Mine a, b;
    std::thread t(&foo, std::ref(a), std::ref(b));
    t.join();

    return 0;
}

使用g++ t.cpp -std=c++11 -static -ggdb,并使用Dr. {内存drmemory a.exe调试它,我得到以下输出:

Dr. Memory version 1.6.1739 build 42 built on Feb 23 2014 22:29:25
Dr. Memory results for pid 7400: "a.exe"
Application cmdline: "a.exe"
Recorded 102 suppression(s) from default C:\Users\apple\Desktop\drmemory\bin\suppress-default.txt

Error #1: LEAK 32 direct bytes 0x00d70e70-0x00d70e90 + 32 indirect bytes
# 0 replace_calloc                      [e:\b\build\slave\win-builder\drmemory\common\alloc_replace.c:2338]
# 1 pthread_spin_init                   [C:\Users\apple\Desktop/t.cpp:22]
# 2 static_spin_init                    [C:\Users\apple\Desktop/t.cpp:22]
# 3 pthread_getspecific                 [C:\Users\apple\Desktop/t.cpp:22]
# 4 _Unwind_SjLj_Register               [C:\mingw482\i686-482-posix-sjlj-rt_v3-r2\build\gcc-4.8.2\i686-w64-mingw32\libgcc/./gthr-default.h:719]
# 5 msvcrt.dll!_endthreadex
# 6 msvcrt.dll!_endthreadex
# 7 KERNEL32.dll!BaseThreadInitThunk   +0x11     (0x77021174 <KERNEL32.dll+0x51174>)

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      0 unique,     0 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total GDI usage error(s)
      0 unique,     0 total warning(s)
      1 unique,     1 total,     64 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     28 unique,    28 total,   1215 byte(s) of still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Users\apple\Desktop\drmemory\drmemory\logs\DrMemory-a.exe.7400.000\results.txt

a.exe本身的输出是(正确地)“Ctor叫”* 2和“Dtor叫”* 2.我看不到任何可能导致泄漏的内容。

P.S。我已经尝试了mingw-gcc 4.8.2的dwarf2和sjlj版本,两者都产生了相同的结果。

P.P.S。我刚尝试了boost线程库,但仍然有类似的结果。如果有人能用MSVC试试,我真的很感激。

1 个答案:

答案 0 :(得分:0)

通常–ignore_early_leaks会有所帮助,否则如果您能证明这确实不是泄漏(并且看起来不是这样) - 正如评论中所建议的那样,您将需要-suppress应用程序中的这些错误。请参阅文档中的抑制格式示例。