Link to home
Start Free TrialLog in
Avatar of Stapman
Stapman

asked on

Exiting my Application correctly...

I have a problem with exiting from my application.
In the InitInstance of my Application (CProgramApp)  class I do a DoModal on a CDialog (CProgramDlg).

*** InitInstance code snip ***
m_pMainWnd = dlg = new CProgramDlg();
int nResponse = dlg->DoModal();
if (nResponse == IDOK)
{            
      ;
}
else if (nResponse == IDCANCEL)
{
      exit (0);
}
***

When my code has run to the end in my CProgramApp and I type
dlg->EndDialog (0);
exit (0);
 
I get an error "user breakpoint at...." and if I debug I can see this assertion failure:
****
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
ASSERT(pWnd != NULL);
****
(pWnd is a null pointer here!)

What did I forget???
Avatar of Stapman
Stapman

ASKER

Edited text of question.
You should not call exit() from InitInstance().  Rather return FALSE if the user cancels the dialog:

   else if (nResponse == IDCANCEL)
   {
      return FALSE;
   }

This will tell MFC to shut down the application.

-- Zizzer
Avatar of Stapman

ASKER

Thanks for that advice but I also have these problems when I do not cancel and press OK.

After compiling CProgrammApp and executing it the Dialog from CProgramDlg appears. Now I press OK and everything works fine until I reach the point where I want to exit.

The syntax
***
dlg->EndDialog(0);
exit(0);
***
aren't situated in the function InitInstance but later in a different function.

thanks anyway...
Okay.  I'll look into this.  You can go ahead and reject my answer.
What is the point of setting your m_pMainWnd to the address of the dialog??

m_pMainWnd = dlg = new CProgramDlg();
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stapman

ASKER

Thanks! I think that should solve that problem. If you could just quickly tell me something in connection to the original question:
I have a result from my DoModal named "nResponse". Here I check if the result is IDOK or IDCANCEL. In my dialog I deleted the original OK button and inserted a button name Button1. In my CProgramDlg the function OnButton is executed and runs the functions in my CProgramApp class. The dialog should close after clicking on that button.

How Do I close the dialog after clicking Button1?
Avatar of Stapman

ASKER

Ok -> Solved the "problem" myself.

Thanks to all.