Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Programmatic application close

PalmpjePalmpje Posts: 51Member
[b][red]This message was edited by Palmpje at 2005-8-25 11:23:10[/red][/b][hr]
This one beats me...

I want to close my app programmatically (as opposed to File/Exit or hitting the 'x' at the top right). I can do this by say using PostQuitMessage(0) and it works, but.... I get a long list of memory leaks whereas I did not allocated any memory at all. The only thing in there is the Doc and the View class.
If I close the program with the File/Exit command the memory leaks do not appear.
I tried checking whether the destructor of the View is called and it is not called when I use PostQuitMessage() but it is called on a 'normal' exit. Could not find any clues yet.

I'm probably being very blond but any help would be appreciated.


Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    you can try PostMessage(WM_CLOSE,0,0), but I think PostQuitMessage(0) posts WM_QUIT message.
  • DB1DB1 Posts: 1,142Member
    : you can try PostMessage(WM_CLOSE,0,0), but I think PostQuitMessage(0) posts WM_QUIT message.
    :

    Yea I read somewhere you need to post 3 messages I think. WM_CLOSE, WM_DESTROY, and WM_NCDESTROY. Some googling might be in order.



    PostQuitMessage is posted in response to a WM_DESTROY message.


    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • tom_swtom_sw Posts: 828Member
    : you can try PostMessage(WM_CLOSE,0,0), but I think PostQuitMessage(0) posts WM_QUIT message.
    :
    Posting WM_CLOSE appears to be the correct way to do this:

    From a quit look at WM_CLOSE and PostQuitMesage in MSDN, it looks like the order is to PostMessage(WM_CLOSE) which will alert the app to close, so it can do clean up, then the app it will PostQuitMessage to tell the system it is ready to be ended. You can create an OnClose handler to do things like verify that you want close - I believe that handler is called called in response to the WM_CLOSE. The MFC framework will automatically post the quit message after the OnClose handler, and you can create an OnDestroy handler to do any last minute cleanup of your CWnd objects (turn off Timers & stuff) in response to that.

    Caveat emptor: It's been quite awhile since I dug into the detail of the app closing chain.


  • PalmpjePalmpje Posts: 51Member
    : : you can try PostMessage(WM_CLOSE,0,0), but I think PostQuitMessage(0) posts WM_QUIT message.
    : :
    : Posting WM_CLOSE appears to be the correct way to do this:
    :
    : From a quit look at WM_CLOSE and PostQuitMesage in MSDN, it looks like the order is to PostMessage(WM_CLOSE) which will alert the app to close, so it can do clean up, then the app it will PostQuitMessage to tell the system it is ready to be ended. You can create an OnClose handler to do things like verify that you want close - I believe that handler is called called in response to the WM_CLOSE. The MFC framework will automatically post the quit message after the OnClose handler, and you can create an OnDestroy handler to do any last minute cleanup of your CWnd objects (turn off Timers & stuff) in response to that.
    :
    : Caveat emptor: It's been quite awhile since I dug into the detail of the app closing chain.
    :
    :
    :

    Hm....

    PostMessage(WM_CLOSE,0,0) hangs the system....
    PostQuitMessage(0) causes the memory leaks...

    I also tried:
    pDoc->OnCloseDocument();
    and this appeared to be very nice except that it returns to the View object...

    I'm going to try to work from the document instead of the view. That might help. To give you some clues: I'm trying to write an interpreter type of thing but obviously Windows based. So I have a view that accepts typed input and if it's a certain command it should close the app.
    I'll see if I can send the keystrokes to the doc and make that interpret the commands...
    Maybe I need an edit view instead of a generic view.

    Back in the old DOS days... programming was plain easy ;-)
  • TribalTechTribalTech Posts: 74Member
    I don't know if this will be any help to you...
    I have closed an application programatically once before. It was a dialog based app. and I forced an OnCancel() from the dialog object. This was done at the end of a time-out. It closed with no problem. I don't know how or if this applies to SDI/MDI Mainfrm objects. I don't think closing your app. from a View or Document object is the way to go. Experiment with closing (cancelling ?) from your Mainfrm.
    Good luck,
    TribalTech
  • PalmpjePalmpje Posts: 51Member
    : I don't know if this will be any help to you...
    : I have closed an application programatically once before. It was a dialog based app. and I forced an OnCancel() from the dialog object. This was done at the end of a time-out. It closed with no problem. I don't know how or if this applies to SDI/MDI Mainfrm objects. I don't think closing your app. from a View or Document object is the way to go. Experiment with closing (cancelling ?) from your Mainfrm.
    : Good luck,
    : TribalTech
    :

    Thanks for all the help. I was also leaning towards the main frame.
    After some tinkering, the obvious (???) worked. From the command parser in the view class I call:

    AfxGetMainWnd()->PostMessage(WM_CLOSE);

    This causes the View destructor to be called, then the Doc destructor and no complaints about memory leaks from the debug session.

    Note that PostMessage(WM_CLOSE) sort of hangs the program.

    Now for the hard parts of the project. Actually parsing commands and executing them ;-)
  • buggermebuggerme Posts: 15Member

    : I want to close my app programmatically (as opposed to File/Exit or hitting the 'x' at the top right). I can do this by say using PostQuitMessage(0) and it works, but.... I get a long list of memory leaks whereas I did not allocated any memory at all. The only thing in there is the Doc and the View class.

    PostQuitMessage(xpto) is Ok... but clean your mess... use ExitInstance() for that...

    [code]
    /*! Exit Instance() //#regiondesc: Override Function

    rief
    Exits App Instance

    Terminates instance, this override is needed because some actions must be
    done at exit time.
    Called by the framework from within a rarely overridden Run member function to
    exit this instance of the thread, or if a call to InitInstance fails.


    eturn
    The thread's exit code; c 0 indicates no errors, and values greater than c 0
    indicate an error.
    This value can be retrieved by calling GetExitCodeThread.


    ote
    Set to public because its needed outside the class ie: called also by
    single instance check )

    MS Link
    */
    //#endregion desc.
    int ClNOApp::ExitInstance()//#region
    {
    // Add your specialized code here and call the base class.

    //! Saves Prefs before exiting...
    PrefsKeep();// no GUI

    // clean your trash
    // if member exists then delete it
    if ( 0 != m_pClDialogFileHasher )
    {
    delete m_pClDialogFileHasher;
    }

    return CWinApp::ExitInstance();
    }
    [/code]

    [code]
    /*! Expected Error() //#regiondesc: Operational Method

    Function to deal with informing the user of possible errors and then
    exit the App.

    param[ in ]
    CString& c a_sErroMessage -> the message to be shown. set as c 0 for generic
    message.

    */
    //#endregion desc.
    void ClNOApp::ExpectedError( const CString& a_sErroMessage ) const///#region
    {
    if ( (a_sErroMessage == C_TXTEMPTY) )
    {
    AfxMessageBox( C_TXTUNEXERROR, MB_OK | MB_ICONERROR );
    }
    else { AfxMessageBox( a_sErroMessage, MB_OK | MB_ICONERROR ); }

    PostQuitMessage( 10 );
    }
    [/code]

    *---*
    http://en.wikibooks.org/wiki/Programming:C_-/-_-/-

Sign In or Register to comment.