Reputation: 31
I have a problem with using the winapi, or so I'm guessing anyway. When I close the application, the process still stays as a 'ghost'. The window disappears as usual, but the process stays which prevents me from recompiling.. I have to kill it manually from the task manager.
I've no clue which part of the code is wrong, and it's somewhat lengthy, so here's a link to the full code: http://pastebin.com/TmRiCeR4
My guess though, is that it might have something to do with this loop:
while(1) {
if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
if(msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
} else
draw();
Sleep(100); //crappy way of stopping 10000000000000 loops a second
}
I guess it doesn't properly 'escape' the loop so it never gets to the closewindow() function right after it, but I might be wrong. I partially copied the code from a tutorial, although I did change it, so I don't see what the problem is from the tutorial code. Here's the tutorial I'm talking about (perhaps outdated?): http://bobobobo.wordpress.com/2008/02/11/opengl-in-a-proper-windows-app-no-glut/
Google didn't give me an answer either. Any ideas?
Upvotes: 0
Views: 173
Reputation: 37122
You haven't shown the code that actually posts the quit message, but if it's a simple PostQuitMessage(0);
then your problem is in your PeekMessage()
call:
if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
By supplying a window filter (hwnd
) you are preventing PeekMessage()
from retrieving any posted thread messages (that is, messages sent not to a window, but to the thread). Since PostQuitMessage()
posts a thread message, your loop will never retrieve it and so never exit.
You should change your call to pass NULL
for the second parameter.
Upvotes: 1
Reputation: 162164
When a window is closed a WM_CLOSE
message is generated, which you normally process in the Window Class's message function. The usual reaction is to call PostQuitMessage
which will send a a WM_QUIT
message. In your main application loop you test for WM_QUIT
and when that message arrives, you do all the necessary shutdown and break out of the loop or call ExitProcess
If there's no idle processing to do, instead of sleeping after a PeekMessage
you should use GetMessage
, which sleeps internally until the next message arrives. However an OpenGL animation loop qualifies as idle processing, so you can't do that.
But if your draw call iterates a million times a second you got another problem: Namely you've got no vertical retrace synchronization (V-Sync) enabled in your graphics driver. Enable it!
Since we want our programs to be robust, i.e. if they run on a different user's computer where V-Sync is disables as well, it makes sense though, to limit the iteration count to some reasonable number. This shouldn't be done using a hard Sleep
though. Instead you should measure the time dT
between draw()
iterations and if it's less than, say 5ms (i.e. 200Hz) you should add a sleep of 5 - dT - 1
after the draw.
Upvotes: 0
Reputation: 28370
Change it so that rather than while(1)
int continue = 1;
while(continue > 0)
and
if(msg.message == WM_QUIT) {
continue = 0;
break;
}
I would also shorten the sleep.
Upvotes: 0