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???
StapmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StapmanAuthor Commented:
Edited text of question.
0
ZizzerCommented:
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
0
StapmanAuthor Commented:
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...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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

m_pMainWnd = dlg = new CProgramDlg();
0
ZoppoCommented:
Hi Stapman,

You do not need to call EndDialog() after DoModal() returns. EndDialog is just used to exit from a dialog while the dialog is active. Just remove the EndDialog and this is allright.

Other (minor) problem is that you should release the memory instantiated for the dialog (the 'new' line). For this call something like 'delete dlg' after DoModal() returns.

On other side for this example it's not neccesarry to allocate and release the dialog on the heap, it would be enough to do just something like this:

CProgramDlg dlg;// allocates the object on the stack, will be release when function terminates
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal(); // DoModal() creates and destroys the dialog
if (nResponse == IDOK)
{
;
}
else if (nResponse == IDCANCEL)
{
exit (0);
}


hope that helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StapmanAuthor Commented:
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?
0
StapmanAuthor Commented:
Ok -> Solved the "problem" myself.

Thanks to all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.