Click here to Skip to main content
15,500,922 members
Please Sign up or sign in to vote.
3.12/5 (4 votes)
See more:
I am trying to exit an MFC app from within the OnTimer() of a CWnd window (not the CFrameWnd). When I call PostQuitMessage(0), the app exits OK but with lots of undestroyed objects, including my CDocument object. If I exit using File/Exit, there are no memory leaks.

Any suggestions what I might be doing wrong? Is there a better way to cleanly exit?


Sergey Alexandrovich Kryukov 9-Nov-13 19:56pm     CRLF
Why doing so? —SA
softwaremonkey 22-Nov-13 10:33am    
If my client application does not get a reply from the server within 10 seconds then something is wrong and I would like to exit rather than just stay open and doing nothing.

In MFC the app exits when the main window gets destroyed. Just simulate main window close. Post the main window a WM_CLOSE message. This works even from another process. Of course this way the main window has the chance to handle WM_CLOSE and to cancel the main window close (for example to popup a "do you want to save the document?" msgbox) but that is natural. You aren't supposed to abruptly terminate a process.
Share this answer
Sergey Alexandrovich Kryukov 9-Nov-13 19:56pm     CRLF
The only correct answer so far, a 5. —SA
pasztorpisti 10-Nov-13 8:30am    
Thank you!
Sergey Alexandrovich Kryukov 22-Nov-13 11:10am     CRLF
The whole idea is wrong. Please see OP's comment to the question quoted in my answer. —SA
softwaremonkey wrote:
If my client application does not get a reply from the server within 10 seconds then something is wrong and I would like to exit rather than just stay open and doing nothing.
I don't think such feature would be beneficial in any way. Probably, you did not think on such situations thoroughly.

Your application should really stay doing nothing, until the user attends to it. It should show an error condition in sufficient detail and wait for the user. Just think about what you suggested: it won't provide the user with essential feedback. Disappearing of the application without user's consent is generally totally confusing, should never be done.

Share this answer
softwaremonkey 22-Nov-13 11:45am    
Thanks for the suggestion. My app gives a clear message explaining that something has not been configured correctly (wrong port or computer name). These problems need investigating and from past experience, if the client sits there after the user has closed the warning dialog, they walk away thinking everything is fine. These are the reasons I would prefer to exit but I accept there may be other (better?) options.
Sergey Alexandrovich Kryukov 22-Nov-13 12:27pm     CRLF
You listed more really clear reasons for not exiting the application. Can't you see that? :-) —SA
softwaremonkey 22-Nov-13 13:27pm    
Hmmm, so let the client run even though it is not receiving valid data and the user has no idea that this is the case. Not sure this is a good idea but its a viewpoint.
pasztorpisti 22-Nov-13 12:13pm    
+5. The correct user feedback is essential in all cases but many programs behave incorrectly from many aspects. One thing I really dislike on windows is popup dialogs coming out of nowhere in front of my focused window and receiving/processing input events immediately even if I wanted to send them (half second ago...) to the original focus owner. One good example to this is Total Commander background file copy ("Do you really want to delete these readonly files???"). In case of network connection (similarly to many other cases) I prefer to use a modeless status bar that shows the state of the communication. Another thing I like is using colors (green/red) to indicate status, this takes basically zero effort from the user's side to recognize things.
Sergey Alexandrovich Kryukov 22-Nov-13 12:26pm     CRLF
Agree. And thank you. —SA
Just return from the WinMain function. If you have a message loop, use PostQuitMessage to break out. Don't try to exit the process in the middle of execution; it's sloppy.
Share this answer
softwaremonkey 9-Nov-13 11:03am    
Thanks for the reply. As I said in my original post, I am calling PostQuitMessage() but it doesn't appear to destroy all allocated objects.
Sergey Alexandrovich Kryukov 22-Nov-13 11:09am     CRLF
It is sloppy to exit from the application without user consent in all cases, with no exclusions. Please see my answer. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900