Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS Tech Support » Problem with auto delete CExtResizableDialog Collapse All
Subject Author Date
Cyril Chevalier Nov 21, 2003 - 7:59 AM

Hi,

I have created a dialog based on CExtResizableDialog, that must self delete when closing (i.e using a ok or a cancel button).
So I have overloaded the method PostNcDestroy :

void CMyDlg::PostNcDestroy(void)
{
CExtResizableDialog::OnNcDestroy();
delete this;
}

And then, from the OnOK or OnCancel, I call PostNcDestroy (or DestroyWindow).

But now I have an assertion when I click on one of the button: it seems that the button sends a message to its parent that is already destroyed. Then, the call to CExtControlBar::DoCustomModeUpdateControlBars( this ) in CExtResizableDialog::WindowProc failed (line 123 of ExtResizableDialog.cpp).

I transformed this line in the following way:

if (IsWindow(GetSafeHwnd())){
CExtControlBar::DoCustomModeUpdateControlBars( this );
}

All works ok.

Is there another way to do what I want ? Or am i doing something wrong here ?
Thanks for help.

Technical Support Nov 23, 2003 - 4:51 AM

Dear Cyril,

Please do not use the PostNcDestroy() method in the dialog because it is not always invoked by the MFC framework, especially when the dialog is modal. You should use OnOK() and OnCancel() methods instead. These two methods are always invoked before the dialog is destroyed. To destroy dialog, please use the EndDialog() method. For example, if you have a dialog that may be used both as a modal and non-modal window, then the following code can be used:

class CTestDlg : public CExtResizableDialog
{
	bool m_bMeIsModal:1;
	void _PreHandleDestruction();
	void _PostHandleDestruction();
public:
	CTestDlg(  // standard constructor
		CWnd* pParent = NULL
		);
	virtual int DoModal();
	virtual BOOL OnInitDialog();
	virtual void OnOK();
	virtual void OnCancel();
	. . .
};

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CExtResizableDialog(CTestDlg::IDD, pParent)
	, m_bMeIsModal( false )
{
}

BOOL CTestDlg::OnInitDialog()
{
	VERIFY( CExtResizableDialog::OnInitDialog() );
	// use default profile
	// (but you may setup a new profile
	// for your dialog here)
	g_CmdManager->ProfileWndAdd( NULL, m_hWnd );
	CExtResizableDialog::AddAnchor( . . . );
	CExtResizableDialog::EnableSaveRestore( . . . );
	. . .
	return TRUE;
}

int CTestDlg::DoModal() 
{
	m_bMeIsModal = true;
	return CExtResizableDialog::DoModal();
}

void CTestDlg::_PreHandleDestruction()
{
	g_CmdManager->ProfileWndRemove( m_hWnd );
}

void CTestDlg::_PostHandleDestruction()
{
	if( ! m_bMeIsModal )
		delete this;
}

void CTestDlg::OnOK() 
{
	_PreHandleDestruction();
	CExtResizableDialog::OnOK();
	_PostHandleDestruction();
}

void CTestDlg::OnCancel() 
{
	_PreHandleDestruction();
	CExtResizableDialog::OnCancel();
	_PostHandleDestruction();
}

Cyril Chevalier Nov 24, 2003 - 6:56 AM

Hi,

Thank you for your quick answer.
I forgot to say that my dialog box is always non modal. That’s why I use the PostNcDestroy solution as found in MSDN library for this case.

I tried your solution but, I still have the same problem. The program asserts in the debug version. What is weird is that when I launch a debug session with some breakpoints, all works fine.

I think there’s a problem with the window messages. It seems that the OnClick message of the ok or cancel button is processed after the window is actually deleted.

I can send you a more detailled example with source code if you like.
Thank you for your help.

Technical Support Nov 24, 2003 - 10:47 AM

Dear Cyril,

the problem is really weird. Dialog window can not receive messages after it was destroyed (including messages sent from its buttons). Yes, please let us take a look at your source code.

Cyril Chevalier Nov 24, 2003 - 6:47 AM

Hi,

Thank you for your quick answer.
I forgot to say that my dialog box is always non modal. That’s why I use the PostNcDestroy solution as found in MSDN library for this case.

I tried your solution but, I still have the same problem. The program asserts in the debug version. What is weird is that when I launch a debug session with some breakpoints, all works fine.

I think there’s a problem with the window messages. It seems that the OnClick message of the ok or cancel button is processed after the window is actually deleted.

I can send you a more detailled example with source code if you like.
Thank you for your help.