我在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
进行编译,然后使用 drmemory a.exe
通过 Dr. Memory 进行调试,我得到了以下输出:
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 called”*2和“Dtor called”*2。我没有看到任何可能导致泄漏的东西。
附言:我尝试了mingw-gcc 4.8.2的dwarf2和sjlj版本,两者产生了相同的结果。
另附言:我刚刚尝试了boost线程库,结果仍然类似。如果有人能使用MSVC尝试一下,我将不胜感激。