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.
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. :(
The debug output from my program, and the code are below.
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<boost::unknown_exception> at memory location 0x0019eed4..
//thread B
...
try
{
SendCallback(hwnd, boost::bind(&Graphics::create, this));
}
catch(fllib::exception::Error &except)//example catch block, doesnt catch example exception
{
...handle exception...
}
void SendCallback(HWND hwnd, boost::function<void()> call)
{
boost::exception_ptr *except_ptr =
(boost::exception_ptr*)SendMessage(hwnd, WM_CALLBACK, (unsigned)&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<void()> *call = (boost::function<void()>*)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...");
}