0

Possible Duplicate:
Should “delete this” be called from within a member method?

Is is correct to call delete from OnOk event handler function, as in the code below.

void CTestDlg::OnOK() 
{
    CDialog::OnOK();

    this->DestroyWindow();
    delete this;
}
Community
  • 1
  • 1
Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85

1 Answers1

1

I presume you're trying all that because the dialog instance is modeless?

In that case you shouldn't be calling the base class OnOK() from your override, but only DestroyWindow().

If you have to delete this then an override of PostNcDestroy() is the appropriate place.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • Can you please explain, why not calling CDialog::OnOk()? – Vivek Kumar Dec 14 '12 at 11:18
  • OnOK() is really meant for DoModal() whereas yours is modeless and needs to be closed down more appropriately. – acraig5075 Dec 14 '12 at 11:55
  • Will it make any difference, if one call even for Modeless dialog? Sorry if this question irritate you! As I am not getting source file where CDialog::OnOk() is defined. – Vivek Kumar Dec 15 '12 at 11:00