0
votes

I am getting the following debug assertion on closing of the application :

enter image description here I have debugged the flow and found that the assertion is happening in CWnd::DestroyWindow()

// Should have been detached by OnNcDestroy
#ifdef _DEBUG
            ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);

My class is derived from COleControl which in turn is derived from CWnd.

The object creation is happening in .NET winform and on Closing of application the object destructor is getting called and the assert is coming.

Things I have tried :

1) Calling DestroyWindow() on my class destructor : didnt work

2) Overridden OnFinalRelease like below and it worked :

void CSimple::OnFinalRelease()
{
    if (!m_bFinalReleaseCalled)
    {
        m_bFinalReleaseCalled = TRUE;

        ReleaseCaches();

        CWnd::OnNcDestroy(); --> explicitly called OnNcDestroy()

        if (m_hWnd != NULL)
            DestroyWindow();

        CCmdTarget::OnFinalRelease();
    }

I am not sure whether this is proper fix. I am also not sure whether the issue is in .NET side.

2

2 Answers

0
votes

Calling CWnd::OnNcDestroy() from other functions is wrong. Generally, these functions are meant to respond to window messages. In this case, when your window is destroyed, the system will send WM_NCDESTROY message (this will be the last message before window is completely destroyed)

You can add ON_WM_NCDESTROY to your window message map. And add this function:

void CMyWnd::OnNcDestroy() 
{
    CWnd::OnNcDestroy(); 
    ... do other cleanup
}

In this case, you might try instead:

void CSimple::OnFinalRelease()
{
    if (m_hWnd != NULL)
        DestroyWindow();
    else
        PostNcDestroy(); // add this
    ...
}
0
votes

You should never call OnNcDestroy before you destroy the window. You should never call this handler by yourself. It is a message only called internally from Windows.

OnNcDestroy is always called when DestroyWindow is called. OnNcDestroy is the handler of the WM_NCDESTROY message and it is the last message a window receives. This function always call PostNCDestroy, that finally removes the window handle from the internal handle maps.

The call to DesroyWindow must fail in this sequence...