基本的に、あるスレッドが別のスレッドが処理する必要がある例外をスローする状況があります。私はブースト例外でこれをやろうとしていますが、行のどこかで型が失われてcatchブロックに捕らえられません。C++:boost :: exceptionのクロススレッド例外処理の問題
基本的にスレッドBは何かしたいと思っていますが、いろいろな理由でスレッドAで処理する必要があります(その理由を知りたいのであれば、なぜdirect3d 9デバイスを作成してリセットし、ウィンドウを作成した)。これらのアクションを実行している間に例外が発生すると、スレッドAはそれをキャッチしてスレッドBに渡し、スレッドBはそれを再発行して必要に応じて処理します。問題は、スレッドBにスローされた例外は、スレッドA. :(
私のプログラムのデバッグ出力、およびコードは以下の通りに投げられたものとは異なるように思われることである。
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...");
}
@Neil ButterworthなぜVSデバッグ出力の一部を削除しましたか? –
申し訳ありません - 私はちょうどタイトルを修正したと思った。 –
あなたはどのように例外をスローしますか? – bayda