Click to See Complete Forum and Search --> : Modeless dialog appears, then dies


Kevin Kelly
August 30th, 1999, 09:55 AM
I have an originally modal dialog I am trying to display modeless like this:
CCatViewDlg CatViewDlg;
CatViewDlg.Create(IDD_CATVIEW);
CatViewDlg.ShowWindow(SW_SHOW);

The dialog box appears on the screen, processes OnInitDialog(), the controls are visible briefly, then the dialog shuts down. Has anybody else encountered this problem and found a solution?

Kevin

Jamie Thornback
August 30th, 1999, 10:32 AM
Your problem is most likely caused by your CCatViewDlg object going out of scope. When the end of the current code block is reached, CatViewDlg will be destroyed, which means that the CDialog destructor will be called. If you look at CDialog::~CDialog, you'll see that it calls DestroyWindow.

To get your dialog to remain active, you'll have to allocate memory for it using new. Then you can either hold onto the pointer and delete it at the appropriate time, or (and this is the preferred choice) you can override a CDialog method like PostNcDestroy to delete the this pointer. (PostNcDestroy is called after the window has been destroyed, so it's a good place to destroy your object.)