DestroyWindow ASSERT problem....strange, strange!
Hi
I have derived a dialog-class in a DLL (shared MFC)
I can construct an object of this class in my app, and when overloading
DoModal() and adding an AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
call, I can see the CMyDialog dialog.
But here is the issue, I have a timer set for a certain time, it posts
a message and the dialog has to be killed - so I use CDialog::OnCancel.
Then DoModal calls DestroyWindow() -
Here a copy of the m_hWnd is made in hWndOrig.
#ifdef _DEBUG
hWndOrig = m_hWnd;
#endif
Then m_hWnd gets set to Null by ::DestroyWndow (which calls
OnNcDestroy).
But later in DestroyWindow itself, there is a check:
#ifdef _DEBUG
ASSERT(m_hWnd == hWndOrig);
#endif
And the program asserts here, which is expected as the ::DestroyWindow
call before this set the m_hWnd to NULL......this is strange!
Please advise, I saw similar queries posted before, but no answers..I
have pasted the Wincore.cpp DestroyWindow() code below for reference...
Thanks!
if (m_hWnd != NULL)
{
pMap = afxMapHWND();
ENSURE(pMap != NULL);
pWnd = (CWnd*)pMap->LookupPermanent(m_hWnd);
#ifdef _DEBUG
hWndOrig = m_hWnd;
#endif
}
#ifdef _AFX_NO_OCC_SUPPORT
if (m_hWnd != NULL)
bResult = ::DestroyWindow(m_hWnd);
#else //_AFX_NO_OCC_SUPPORT
if ((m_hWnd != NULL) || (m_pCtrlSite != NULL))
{
if (m_pCtrlSite == NULL)
bResult = ::DestroyWindow(m_hWnd);
else
bResult = m_pCtrlSite->DestroyControl();
}
#endif //_AFX_NO_OCC_SUPPORT
if (hWndOrig != NULL)
{
// Note that 'this' may have been deleted at this point,
// (but only if pWnd != NULL)
if (pWnd != NULL)
{
// Should have been detached by OnNcDestroy
#ifdef _DEBUG
ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);
#endif
}
else
{
#ifdef _DEBUG
ASSERT(m_hWnd == hWndOrig);
#endif
// Detach after DestroyWindow called just in case
Detach();
}