How to overwrite ID_APP_EXIT function ?

Posted on 2004-11-24
Last Modified: 2013-11-20

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?

Question by:l_madhavi
    LVL 19

    Expert Comment

    >> 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

    LVL 39

    Accepted Solution


    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now