Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreaded Win32 C++ program crashes using try/catch in multiple threads
    primarykey
    data
    text
    <p>I'm writing a multithreaded program, which would crash when a particular exception was thrown. Having stripped out more and more of the code to find the problem, I'm now left with an extremely simple case which causes one of many different crashes, seemingly at random. I'm at a loss as to why it's doing this.</p> <p>Here's the entire contents of the program as it stands:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;windows.h&gt; WINAPI DWORD threadFunc(LPVOID x) { while (true) { try { throw 1; } catch (...) { } } return 0; } int main(int argc, char *argv[]) { CreateThread(NULL, 0, threadFunc, NULL, 0, NULL); CreateThread(NULL, 0, threadFunc, NULL, 0, NULL); Sleep(1000); return 0; } </code></pre> <p>As far as I can tell, the program crashes while trying to throw, but not necessarily the first time.</p> <p>The errors I'm getting in debug mode are:</p> <ul> <li>seg fault, with stack trace including: <ul> <li>00403F70 _Unwind_SjLj_RaiseException(exc=0x474380) (../../../gcc-4.4.1/libgcc/../gcc/unwind.inc:113)</li> <li>00000000 0x004025f6 in __cxa_throw() (??:??)</li> <li>00401380 threadFunc(x=0x0) (D:\Software Projects\testcpp\main.cpp:6)</li> <li>7C80B729 KERNEL32!GetModuleFileNameA() (C:\WINDOWS\system32\kernel32.dll:??)</li> <li>00000000 0x00000000 in ??() (??:??)</li> </ul></li> <li>Program exited with code 03</li> </ul> <p>and running without the debugger:</p> <ul> <li>"The instruction 0x <em>something</em> referenced memory at 0x <em>something else</em>", sometimes once, sometimes twice (presumably for the second thread)</li> <li>"Process returned -1073741819 (0xC0000005)", which is an access violation</li> <li>"This application has requested the Runtime to terminate it in an unusual way ... Process returned 3 (0x3)"</li> </ul> <p>I'm completely stumped as to what could be causing all these different errors in such a short program. Commenting out one of the CreateThreads stops any errors occurring, so it seems to be something to do with the interaction of multithreading and exception throwing.</p> <p>I'm using 32-bit Windows XP SP3, and MinGW 4.4.1</p> <h1>Update</h1> <p>The problem seems to have been a bug in the compiler (which is TDM-2 mingw32 4.4.1 - I wasn't aware of the TDM element when asking the question, but I don't think it makes a difference). Having upgraded to version 4.6.1 instead, it all seems to work fine.</p> <p>Thanks to all who contributed to this question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload