How to overwrite ID_APP_EXIT function ?


I am working with MDI application. I want to ovverwrite ID_APP_EXIT, File->Exit menu function of Mainframe menu. When I click on this menu option or on the close icon button of Main frame, I should check for a particular condition and then allow to exit the application if the condition is true or else I should not allow the user to exit.

I am able to overwrite and check the condition, but unable to close the application if condition is met. If I used PostQuitMessage(0), it's getting closed but I'm getting lot of memory leaks due to this function.

Can anyone please give some inputs?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> it's getting closed but I'm getting lot of memory leaks due to this function.

You are probably not waiting for a thread to terminate if you have any running.  Check this, and then try

evilrixSenior Software Engineer (Avast)Commented:

PostQuitMessage() terminates the applications message pump, which has the side effect of terminating the application but this isn't really the best way. Normal frame closedown messages (such as WM_CLOSE) are skipped.

I've found it is generally easier to override the OnClose() message handler for WM_CLOSE rather than OnAppExit(). When ID_APP_EXIT is send to the main window it generates a WM_CLOSE message, which is fired to the main frame. This will invoke the OnClose() function. In here you can place your closedown code. If you need to prevent termination of the application just don't call the base class implementation of OnClose().

For example: -

void CMainFrame::OnClose()
      BOOL bOkToClose = this->CloseDownFunc();

The other advantage of placing your code in this function is that it will trap ALL attempts to close your application (e.g. via the Windows close gadget). This would not be the case if you use OnAppExit() as this will only trap attempts to exit via the File/Exit menu item.

I hope this helps.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.