Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Cross thread exception handling problem with boost::exception
    text
    copied!<p>Basically, I've got a situation where one thread throws an exception which a different thread needs to handle. I'm trying to do this with boost exception, however somewhere along the line the exception loses its type and thus isn't caught by the catch blocks.</p> <p>Basically, thread B wants to do something, however for various reasons it must be done with thread A (if you want to know those reasons ask MS why a direct3d 9 device must be created, reset and releashed by the same thread that created the window). If, while carrying out those actions, an exception occurs, thread A catches it, passes it back to thread B, which then rethrows it to be handled as needed. The problem is that the exception thrown in thread B seems to be different from the one thrown in thread A. :(</p> <p>The debug output from my program, and the code are below.</p> <pre> First-chance exception at 0x776b42eb ...: fllib::exception::Error at memory location 0x0019e590.. First-chance exception at 0x776b42eb ...: [rethrow] at memory location 0x00000000.. First-chance exception at 0x776b42eb ...: boost::exception_detail::clone_impl&lt;boost::unknown_exception&gt; at memory location 0x0019eed4.. </pre> <pre><code>//thread B ... try { SendCallback(hwnd, boost::bind(&amp;Graphics::create, this)); } catch(fllib::exception::Error &amp;except)//example catch block, doesnt catch example exception { ...handle exception... } void SendCallback(HWND hwnd, boost::function&lt;void()&gt; call) { boost::exception_ptr *except_ptr = (boost::exception_ptr*)SendMessage(hwnd, WM_CALLBACK, (unsigned)&amp;call, SEND); if(except_ptr)//if an exception occurred, throw it in thread B's context { boost::exception_ptr except = *except_ptr; delete except_ptr; boost::rethrow_exception(except); } } //thread A LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { //check for our custom message if(msg == WM_CALLBACK) { if(lParam == POST) { ... } else { boost::function&lt;void()&gt; *call = (boost::function&lt;void()&gt;*)wParam; try { (*call)(); } catch(...) { return (unsigned)new boost::exception_ptr(boost::current_exception()); } return 0; } } else { ... } } void Graphics::create() { ...code that may throw exceptions... eg throw fllib::exception::Error(L"Test Exception..."); } </code></pre>
 

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