title: Crash when using boost::timed_join and FreeLibrary
body: <p>I require the use of <code>LoadLibrary</code> and <code>GetProcAddress</code> to call functions from some dlls (which plug into my app), but I don't want them to be able to hang my program by containing an infinite loop or hanging themselves, so I'm trying to use <code>boost::thread</code> (no experience) to circumvent that. However, if a function does hang, I crash with ACCESS_DENIED (0x00000005) when calling <code>FreeLibrary</code> to clean up.</p>
  
  <p>Here's a minimal DLL (<code>mydll.dll</code>) that reproduces the problem:</p>
  
  <pre><code>extern "C" {
      void __declspec(dllexport) foo() {
          for(;;); //&lt;-- infinite loop
      }
  }
  </code></pre>
  
  <p>And here's a minimal cpp that crashes. I've left out error checking for brevity, but I can assure you no errors are given:</p>
  
  <pre><code>#include &lt;boost/thread.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  
  int main() {
      using FuncType = void (*)();
  
      HMODULE dll = LoadLibrary("mydll.dll");
  
      FuncType func = (FuncType)GetProcAddress(dll, "foo");
  
      boost::thread thr = [func] {func();};
  
      if (!thr.timed_join(boost::posix_time::seconds(1))) {
          std::cout &lt;&lt; "Timed out.";
          //thr.detach();
          FreeLibrary(dll);
      }
  
      for (;;);
  }
  </code></pre>
  
  <p>Upon running this, "Timed out." is printed and the call stack (gdb on CodeBlocks) at the crash is as follows:</p>
  
  
  
  <pre><code>#0 00000000 0x6c1c1214 in ??() (??:??)
  #1 0040133E operator() (__closure=&lt;optimized out&gt;) (C:\...\thread test.cpp:17)
  #5 00000000 0x00000000 in ??() (??:??)
  </code></pre>
  
  <p>Without optimizations, that becomes:</p>
  
  
  
  <pre><code>#0 00000000 0x6c1c1214 in ??() (??:??)
  #1 00401348 operator() (__closure=0x898f90) (C:\...\thread test.cpp:17)
  #2 00401780 boost::detail::thread_data&lt;main()::&lt;lambda()&gt; &gt;::run(void)(this=0x898f70) (c:/.../boost/thread/detail/thread.hpp:62)
  #5 00000000 0x00000000 in ??() (??:??)
  </code></pre>
  
  <p>The first thing I tried was adding in the <code>thr.detach();</code> line that I commented out above, but that produced the same result.</p>
  
  <p>I've also tried the following to no avail:</p>
  
  <ul>
  <li>Replacing the lambda with a function (I'm not sure how well Boost fits in with C++11 stuff) --- crashes</li>
  <li>Commenting out the <code>FreeLibrary</code> line --- works fine, but I'd rather free it after loading it, even if it is reference-based. I'm also cautious about that kind of behaviour working later on.</li>
  <li>Replacing the infinite loop in the DLL with one that returns --- works fine, but I can't count on that</li>
  <li>Not using <code>LoadLibrary</code> or anything and inlining the infinite loop --- works fine, but I need to use DLLs:</li>
  </ul>
  
  <blockquote>
    <p>boost::thread thr = []{for (;;);};</p>
  </blockquote>
  
  <p>The DLL and cpp were both compiled on Windows 7 64-bit using GCC 4.7.1 with 32-bit output types. My version of Boost is 1.49.0. If memory serves, I think I got one or two tests in on Windows XP 32-bit (same compiler etc) before moving to a different computer as well.</p>
  
  <p>Why is this crashing? How can I ensure a function I call dynamically doesn't hang my program?</p>