title: Does win32 std::thread leak memory?
body: <p>I compiled the following code with MinGW gcc 4.8.2 under Windows 7 32-bit:</p>
  
  <pre><code>// t.cpp
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  class Mine
  {
  public:
      Mine() { std::cout &lt;&lt; "Ctor called\n"; }
      ~Mine() { std::cout &lt;&lt; "Dtor called\n"; }
  };
  
  void foo(const Mine &amp;a, const Mine &amp;s)
  {
  }
  
  int main()
  {
      Mine a, b;
      std::thread t(&amp;foo, std::ref(a), std::ref(b));
      t.join();
  
      return 0;
  }
  </code></pre>
  
  <p>with <code>g++ t.cpp -std=c++11 -static -ggdb</code>, and debug it using Dr. Memory with <code>drmemory a.exe</code>, and I got the following output:</p>
  
  <pre><code>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 &lt;KERNEL32.dll+0x51174&gt;)
  
  ===========================================================================
  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
  </code></pre>
  
  <p>The output of a.exe itself is (correctly) "Ctor called" * 2 and "Dtor called" * 2. I couldn't see anything that could cause a leak.</p>
  
  <p>P.S. I've tried both dwarf2 and sjlj versions of mingw-gcc 4.8.2, both of which produced the same result.</p>
  
  <p>P.P.S. I just tried boost thread library, with still the similar result. I'd really appreciate it if anyone can try it with MSVC.</p>