   Terminating a program...
  Good evening. My level editor is an MFC app, which uses the Document/View architecture. If something critical fails, such as the performance counter initialization or some part of the texture loader, I need the program to terminate immediately. Obviously, if it''s a critical error, the program can''t continue.Now, if this was a normal Win32 program, I would call exit(), having first specified a function to atexit() which would be called to clear up any loose ends like allocated memory before exiting. However, MFC is a different beast, and using exit() might not be the correct way of shutting down my application after an error. I want to use exit() throughout the program (critical functions can potentially be called at any time, not just the start), so should I go ahead and use exit(), and specify a custom function to atexit() which closes down MFC gracefully, or should I use a different method? If I was to choose exit(), what should I call to close down MFC in it''s Document/View architecture state? Thank you for your time.   Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company that can''t stand 1 bit of competition. 
  PostQuitMessage(0) has always served me well, although I''ve never used it in an MFC application. If possible, it''s always best to have a function that gracefully closes the program, just to be safe and all.Try that and let me know how it works. ;  Erm, I thought calling exit() yourself was never a good idea, unless you're initializing the CRT yourself, etc. (doing what WinMainCRTStartup does).  edit: Nevermind, I see that atexit() does whatever I was thinking of.  I've never actually messed with it.Anyway, my solution still stands: why don't you use (structured) exception handling for your problem?  With MFC, you can assume that it's a Windows target platform, and use the additional features of SEH over standard C++ EH.   [edited by - psykr on February 15, 2004 10:34:51 PM];  Thanks for the replies. PostQuitMessage(0) won''t work, as I need the program to terminate immediately after any critical error that occurs. There''s no two ways about it; the program will crash and generate resource leaks if it isn''t terminated.Exception handling is an interesting idea. I''ve seen try { throw } catch() { } blocks before (mainly in my work with MFC), but I''ve never used them. Can someone explain quickly how they work, and how and when I should use them? I''ll go and dig up my C++ book...   Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company that can''t stand 1 bit of competition. ;  A really simple way:In your main entry point (MFCs equivalent of WinMain()): try{   // Main code here}catch(...) {}// Cleanup code hereAnd then when you encounter an error: if(!bSomething) throw 0;;  ExitProcess can be used, it will shutdown DLLs by calling the EP for a proper cleanup. To immidatly kill your process. Use TerminateProcess. 