Sign in to follow this  
Fire Lancer

Error handeling problem

Recommended Posts

In general I use exceptions to deal with errors, however the problem I have here is that the error applies to a diffrent thread than the one that caused it. Basicly the window has its own thread, and the direct3d device must be created and reset by the same thread that created the window. However creating the device may fail, so I need to throw an exception in the thread that was trying to create the instance, not the window code The callback function:
void Callback(HWND hwnd, boost::function<void(HWND,LPARAM)> call, LPARAM lParam)
{
	//Make our stack allocated function object into a heap allocated one
	boost::function<void(HWND,LPARAM)> *callH = new boost::function<void(HWND,LPARAM)>(call);
	//send a message with a pointer to our function object in the WPARAM
	PostMessage(hwnd, WM_CALLBACK, (unsigned)callH, lParam);
}
LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	//check for our custom message
	if(msg == WM_CALLBACK)
	{
		//retreive the function pointer from the WPARAM
		boost::function<void(HWND,LPARAM)> *callH = (boost::function<void(HWND,LPARAM)>*)wParam;
		//call it
		(*callH)(hwnd,lParam);
		//delete our heap allocated function object
		delete callH;
		return 0;
	}
	else
		//if there was nothing relevant to us, call the old message procedure
		return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam);
}
//std::map<HWND, WNDPROC> hooked;

The code that requests the window thread to create the device
Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
:lost(false), reset(false), refCnt(0), backCol(0xFF000000),
started(false), exited(false),
window(_window), size(_size)
{
	window->AddRef();
	HWND hwnd = *((HWND*)window->GetHandle());
	CallbackHook(hwnd);
	Callback(hwnd, boost::bind(&Graphics::create, this), 0);

	while(!started)Sleep(100);
}
void Graphics::create()
{
...code that may throw various exceptions
	started = true;
}

So basicy I somehow need to catch any exceptions in Graphics::create() and rethrow them from the constructor... Is there some way to do this that still lets me go to the source of the exception in the case of an unhandled exception?

Share this post


Link to post
Share on other sites
In terms of twiddling debugger settings, there's Break When an Exception is Thrown.

Quote:
  1. On the Debug menu, click Exceptions.
  2. In the Exceptions dialog box, select Thrown for an entire category of exceptions, for example, Common Language Runtime Exceptions.


Poking around MSDN (not at a computer with VS installed), it looks like 'C++ exceptions' is a category you could do this for.

Of course, if you're not using Visual Studio, this is no help whatsoever!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this