Sign in to follow this  
fpsgamer

[Win API] Shutting down .... Is this safe?

Recommended Posts

fpsgamer    856
I wrote a win32 wrapper class that encapsulates the creation of a window and OpenGL context. When the user presses ESC I post a WM_QUIT message through a call to GLApp::quit(). In the handler for that message I destroy all window components including the OpenGL context through a call to GLApp::shutdown(). Within that function is a call to DestroyWindow() which itself posts a WM_DESTROY message. Lastly the handler for WM_DESTROY posts a WM_QUIT message, I want to be sure that WM_CLOSE, WM_DESTROY and WM_QUIT are handled one after the other before my message handler exits. For example, I want to be sure that no OpenGL drawing calls execute between WM_QUIT and WM_DESTROY, since the WM_QUIT handler would have destroyed the OpenGL context.
void GLApp::quit() const
{
	PostMessage(this->hWnd, WM_CLOSE, 0, 0);
}




LRESULT GLApp::WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{ 
	switch (uiMsg)
	{
                ...
		case WM_CLOSE: // The user clicked the 'x' button
			this->shutdown(); // Eventually calls DestroyWindow(hWnd) which then posts a WM_DESTROY message 
			return 0;
		case WM_DESTROY:
			PostQuitMessage(0); // Post a WM_QUIT message
			return 0;
	}
	return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}




This function destroys windows, contexts and restores the user's display settings.
void GLApp::shutdown()
{
	if(fullScreen){
		ChangeDisplaySettings(NULL, 0);
		ShowCursor(true);
	}

	if(hRC && hDC)
	{
		wglMakeCurrent(hDC, NULL);
		wglDeleteContext(hRC);
		hRC = NULL;
	}
	if(hDC)
	{
		ReleaseDC(hWnd, hDC);
		hDC = NULL;
	}
	if(hWnd)
	{
		DestroyWindow(hWnd);
		hWnd = NULL;
	}
	if(hInstance)
	{
		UnregisterClass(lpszClassName, hInstance);
		hInstance = NULL;
	}
}




Share this post


Link to post
Share on other sites
Trentelshark    122
Once your context is gone it's gone, there is nothing for your OpenGL calls to render to. You could have your application throwing glVertex3f calls all day long but without a context they don't really end up anywhere since OpenGL has no means to communicate with the hardware (you can test this by setting the current context in one thread and making render calls in another).

Your behaviour mimics mine (though I have the OpenGL render running in a seperate thread so I have additional shutdown) and I have not encountered any issues so far.

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