PostQuitMessage()

I got some code from a tutorial and in the WinProc function there is:
1
2
3
4
5
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
}

Is it safe to remove that and put exit(0);, or will that cause memory leaks?
Memory is cleaned up by the scheduler on the OS when your process is done executing so Memory leaks when your process closes are fixed anyway. "PostQuitMessage(...)" does much more then just close the process you're looking at though so I would say go with it over "exit(...)".
Last edited on
When I put PostQuitMessage(...); I have to click the close button multiple times before it does anything. What makes it so important?
Clicking the close button should send a WM_QUIT message into the queue. Do you have something else handeling the WM_QUIT condition?
Problem fixed. I moved PostQuitMessage(...); to WM_QUIT and it closes immediately.
codist wrote:
Problem fixed. I moved PostQuitMessage(...); to WM_QUIT and it closes immediately.


There is something wrong with your code - possibly in the way or the order in which you are
processing the messages to end the program.

Could you post the code??

closed account (z05DSL3A)
WM_QUIT message is generated when the application calls the PostQuitMessage function. So calling PostQuitMessage() is this handler in redundant.

PostQuitMessage() is a typical response to the WM_DESTROY message.

Edit:
Computergeek01 wrote:
Clicking the close button should send a WM_QUIT message into the queue. Do you have something else handeling the WM_QUIT condition?

The close buttons send a WM_CLOSE message. This indicates a window or an application should terminate. Handling this message gives you the chance to intervene,canceling the close/saving data...
Last edited on
So I put an exit(0); in WM_QUIT
No, that doesn't belong there. Post your Message handler loop, it's probably broken if you need to do something like that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
switch(message) {
	case WM_CHAR:
		keyboard[wParam] = true;
		break;

	case WM_DEADCHAR:
		keyboard[wParam] = false;
		break;

	case WM_CREATE:

		hDC = GetDC(hwnd);
		g_HDC = hDC;
		SetupPixelFormat(hDC);



		hRC = wglCreateContext(hDC);
		wglMakeCurrent(hDC, hRC);

		return 0;
		break;

	case WM_CLOSE:

		if (MessageBox(hwnd, "Are you sure you want to quit?t", "Quit?", MB_YESNO | MB_ICONQUESTION) == IDNO)
			return 0;

		wglMakeCurrent(hDC, NULL);
		wglDeleteContext(hRC);

		Shutdown();
		PostQuitMessage(0);

		return 0;
		break;

	case WM_QUIT:
		exit(0); // should I have this here?

	case WM_SIZE:

		height = HIWORD(lParam);
		width = LOWORD(lParam);


		if (height == 0) {
			height = 1;
		}


		glViewport(0, 0, width, height);


		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		gluPerspective(54.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		return 0;
		break;

	default:
		break;
}
This bit here is 'interesting'

1
2
    case WM_QUIT:
        exit(0); // should I have this here? 


It is interesting because (and I'm quoting from Microsoft info):
The WM_QUIT message is not associated with a window and therefore will never be received through a window's window procedure. It is retrieved only by the GetMessage or PeekMessage functions.



Are you force sending the WM_QUIT message using the SendMessage function?


Im interested in what the Shutdown() function does.
Last edited on
codist, I was asking for your handler loop, not for your window procedure. The message handle loop is the thing that looks a little something like this:

1
2
3
4
5
while(GetMessage(&msg,hWnd, 0, 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


Normally your window procedure never receives a WM_QUIT message, because the loop exits when the message reaches WM_QUIT, so DispatchMessage shouldn't be called. If your program doesn't exit after PostQuitMessage, this is the part that is most likely to be the cause.
sorry
1
2
3
4
5
6
7
8
9
10
while (!done) { //done is defined earlier 
PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE);

if (msg.message == WM_QUIT) done = true; // I think this should quit it, but it doesn't seem to do 
//anything unless I click the close button a few times.

render();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Ah, I see. That's why. Do it like I did above (of course you can include your render code, though you should probably put that after dispatch message - actually it would be better to put render() into WM_PAINT, and call InvalidateRect on your window inside of a timer.)
@hanst99 - If I put render() in the handler loop, it goes very slow, even though I'm only drawing a cube. If I put it in the WM_PAINT message, and call InvalidateRect in a timer (with SetTimer()), it glitches up and uses a HUGE amount of memory, and computer usage.
Last edited on
Topic archived. No new replies allowed.