Sign in to follow this  
neiluk

Program not shutting down fully...

Recommended Posts

neiluk    122
Hi, having problems closing my opengl application fully, the window closes but when i so ctrl+alt+del it still shows up in the processes list, running windows xp pro, and heres my closing code: case WM_CLOSE: wglMakeCurrent(NULL, NULL); wglDeleteContext(hRC); ReleaseDC(hwnd, hDC); DestroyWindow(hwnd); PostQuitMessage(0); return 0; break; And im also deleting all my objects using delete after the main loop exits, any ideas what i need to do?

Share this post


Link to post
Share on other sites
Oberon_Command    6075
Quote:
Original post by Xiachunyi
Are you unregistering your class?

"UnregisterClass(ClassName, hInstance)"


That doesn't really help, you know, you can get away with not doing that without the problem he's having...

BTW have you tried running your program in a debugger? Try that, see if you can figure out what's causing the problem that way.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Put the PostQuitMessage in the WM_DESTROY handler. My guess is that PostQuitMessage immediately after DestroyWindow is causing you to shut down your message pump and exit the thread before the window has finished being destroyed.

Share this post


Link to post
Share on other sites
Dim_Yimma_H    557
I can't tell if your wgl shutdown is correct but I am aware of a quite common Win32 shudown problem. Basically the problem arises when one destroys the window but doesn't exit the program code, so it keeps running without the main window.

First in the code a window is created, a message loop is entered and eventually the window is destroyed, then the program code exits. Since you call DestroyWindow the window is destroyed but the program shows up in the process list so it's highly possible the WinMain function was never exited and probably the message loop is still running. Are you sure your main loop does exit?

A generic way to make sure this doesn't happen is to respond to the WM_DESTROY message (which is sent by the DestroyWindow function) and first then make a call to PostQuitMessage(0) which sends a WM_QUIT message.

How to respond to the WM_QUIT message depends on if your message loop is using the GetMessage function or PeekMessage function to be able to pump the messages to the window procedure. GetMessage returns 0 if a WM_QUIT message is recieved, so if that happens break out of the message loop and return from WinMain. If you're using PeekMessage then just test whether msg.message == WM_QUIT, if so break out of the message loop.

Here's an example using GetMessage:

The message loop:

while (true)
{
hasMessage = GetMessage(&msg,
NULL,
0,
0);

//if the WM_QUIT was recieved and hasMessage is 0 then the
//window has already been destroyed
//so we can just exit the message loop
if (hasMessage == 0)
break;

//send the message to the window procedure
DispatchMessage(&msg);
} //message loop ends here

//return the last handled message to the caller (exit the program)
return msg.wParam;
//end of WinMain



In the window procedure:

case WM_CLOSE:
DestroyWindow(hWnd); //destroys and sends a WM_DESTROY message to this window
return 0;

case WM_DESTROY:
//the window is destroyed, now send a WM_QUIT message so the message loop stops
//this is very important otherwise the code won't exit
PostQuitMessage(0); //the only parameter is the return code of the WM_QUIT message
return 0;



Hope that helped somehow.

Share this post


Link to post
Share on other sites
neiluk    122
Nope still cant fix it, this is what ive got:

while(!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

if (msg.message == WM_QUIT)
{
done = true; // Exit the main loop
}
else
{
// Main loop....

}
}
// Delete objects

return (msg.wParam);

And ive moved PostQuitMessage to WM_DESTROY instead of close but made no difference. Im working from code out of the "Opengl game programming" book by the Gamedev.net people if that helps if theres a known problem in their code that does this..?

Share this post


Link to post
Share on other sites
spock    217
Quote:
Original post by neiluk
Nope still cant fix it, this is what ive got:
while(!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

if (msg.message == WM_QUIT)



Assuming this is your main thread you most likely want to call PeekMessage like this instead:

PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);


When you specify a window handle in your call to PeekMessage or GetMessage you only retrieve messages associated with that window, and the WM_QUIT message will be associated with a thread, not a window.

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