[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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???
0
Stapman
Asked:
Stapman
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now