Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC DoModal Dialog

Tags:

c++

mfc

Okay, so I will admit I have no knowledge of windows API or even MFC.

I've got an error window popping up when things go hairy (illegal character in a filename string) and I want the error box to be modal.

For the life of me I can't figure out why it crashes when it hits doModal.

Here is the code where I think this can be fixed. This code is in the event handler of a button in the main window.

CDialog *BadFileD = new CDialog();
BadFileD->Create(IDD_STATUS, this); 
BadFileD->DoModal();

Am I just being borderline retarded?

like image 358
Russbear Avatar asked Jun 30 '11 00:06

Russbear


2 Answers

MFC dialog divides two pattern, modal dialog and modeless dialog.

(1) Modal dialog usage:

CDialog dlg;
dlg.DoModal();

(2) Modeless dialog usage:

CMyDialog *pDlg = new CMyDialog();
pDlg->Create(ID_DLG, this);
pDlg->ShowWindows(SW_SHOW);

As you can see, we need a new pointer, but do not delete it. So, you need to do the following in our CMyDialog class:

  1. Add DestroyWindow() method in OnOk() and OnCancel().
  2. Add "delete this;" in PostNcDestroy() method.

If you do not, your code may cause a memory leak. BadFileD is a class member, and you delete it in destructor. I suggest use Modeless dialog.

like image 132
Jerry Zhang Avatar answered Oct 23 '22 12:10

Jerry Zhang


For display modal dialog you should use DoModal method only

CDialog *BadFileD = new CDialog(IDD_STATUS, this);
BadFileD->DoModal();

You can read remarks from article

like image 10
Victor Avatar answered Oct 23 '22 11:10

Victor