Clean exit for SDI/MDI program
Hi All,
I have a question which am still bothering.
How do you exit a SDI or MDI program clean without corrupting any memory?
In my program has some ActiveX DLLs ..
Thanks for your respond.
Sincerely,
Michael
# 1 Re: Clean exit for SDI/MDI program
That's a biiiig topic ! To exit the program, use PostQuitMessage(0) or something similar. To exit "cleanly", unallocate all dynamicalling allocated memory and close all open files before posting that message.
stober at 2007-11-10 8:54:06 >

# 2 Re: Clean exit for SDI/MDI program
Thanks Sober .. really appreciate it. :)
# 3 Re: Clean exit for SDI/MDI program
Originally posted by Michael Tjong
Thanks Sober .. really appreciate it. :) You should not appreciate advice to use PostQuitMessage to exit a program cleanly.
You want to allow MFC to do it's cleanup. If you put your cleanup in appropriate places then it will be done at the appropriate time automatically and you don't need to do it in processing that specifically bypasses MFC.
You can send a WM_CLOSE message to the main frame. Just one line of source code is probably enough. All cleanup should then happen automatically. If there are any modified documents then MFC will prompt the user to ask if they should be saved.
# 4 Re: Clean exit for SDI/MDI program
What Sam said is true!
WM_CLOSE will post a WM_DESTROY message which inturn posts a WM_QUIT message on the message queue. PostQuitMessage() will only post the WM_QUIT msg (which bypasses MFC own cleanup if you do that).
If you want to exit in the Windows class (in CWnd derived classes), do
PostMessage(WM_CLOSE);
If you want to quit in the middle of your handcoded source or while not inside CWnd derived classes , do
::AfxGetMainWnd()->PostMessage (WM_CLOSE);
# 5 Re: Clean exit for SDI/MDI program
Thanks for the replies .. :)
But it gives me assertion error as shown below:
_AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
{ ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd, message, wParam, lParam); }
Michael
# 6 Re: Clean exit for SDI/MDI program
Originally posted by Michael Tjong
But it gives me assertion error as shown below:But you do not tell us what "it" is or where you are doing what and where the window handle is gotten from.
# 7 Re: Clean exit for SDI/MDI program
But then again what was the memory issue you were running into originally. You state memory corruption (do you mean leak or file corruption), but there are many ways this can occurr not technically related to the application closing.
For example if you create a variable thru the new process like so
OVERLAPPEDPLUS *pOlap;
pOlap = new OVERLAPPEDPLUS;
You now have housekeeping you have to do yourself to make sure you do not leave any memory leaks. So as soon as you no longer need do something like
delete pOlap;
# 8 Re: Clean exit for SDI/MDI program
Hi All,
Thanks for all the responds .. appreciate it :)
Yes, I do get "assertion error" in override function "SaveModified()" in the CDocument.
Once the return "TRUE" of this function, this assertion accurs.
I call "::AfxGetMainWnd()->PostMessage (WM_CLOSE);" from pull-down menu in a CView screen.
What I mean of memory corruption is "memory leak". I am looking for a completely clean exit. Either automatically done by the document or need to manually coded some "cleanings".
Thanks ..
Michael