Debug Assertion failed in .exe/wincore.cpp

6.8k Views Asked by At

I am doing an RT simulator in VC++ 6.0. whenever it is executed, without the Open Architecture Computer(OAC,it is the Bus Controller in the Flight) switched on, the program executes properly. But with the OAC ON, the program is giving Debug assertion failed- in Debug/.exe/wincore.cpp at line no. 980. what may be the problem? Please provide with the solution if possible.

This is the copmlete DestroyWindow function.

BOOL CWnd::DestroyWindow()
{
    if (m_hWnd == NULL)
        return FALSE;

    CHandleMap* pMap = afxMapHWND();
    ASSERT(pMap != NULL);
    CWnd* pWnd = (CWnd*)pMap->LookupPermanent(m_hWnd);
#ifdef _DEBUG
    HWND hWndOrig = m_hWnd;
#endif

#ifdef _AFX_NO_OCC_SUPPORT
    BOOL bResult = ::DestroyWindow(m_hWnd);
#else //_AFX_NO_OCC_SUPPORT
    BOOL bResult;
    if (m_pCtrlSite == NULL)
        bResult = ::DestroyWindow(m_hWnd);
    else
        bResult = m_pCtrlSite->DestroyControl();
#endif //_AFX_NO_OCC_SUPPORT

    // 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
//////////////////////////////HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!///////////////////
        ASSERT(pMap->LookupPermanent(hWndOrig) == NULL); //line 980
#endif
    }
    else
    {
#ifdef _DEBUG
        ASSERT(m_hWnd == hWndOrig);
#endif
        // Detach after DestroyWindow called just in case
        Detach();
    }
    return bResult;
}
2

There are 2 best solutions below

0
On

I think this problem has something to do with using CWnd::FromHwnd inappropriately, like storing the resulting pointer, and using it later. If something must be stored, it should be HWND, not CWnd*.

Another issue might be creating window in one thread and destroying it in another.

0
On

The problem is most likely that somewhere you are calling CWnd::GetSafeHwnd(), and still using that HWND handle at the time the window is being destroyed. In other words, you're destroying a CWnd whose handle is still active somewhere else.

One solution is to override virtual BOOL DestroyWindow(), and make sure you release your handle there.

For example, if you're showing a modal dialog box from an Acrobat plug-in, you have to pass your window handle to Acrobat, to let it know that you're in modal mode:

int CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if(CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;
    // Put Acrobat into modal dialog mode
    m_AVdlgWin = AVWindowNewFromPlatformThing(AVWLmodal, 0, NULL, gExtensionID, GetSafeHwnd());
    AVAppBeginModal(m_AVdlgWin);
    AVWindowBecomeKey(m_AVdlgWin);
    return 0;
}

Of course you need to perform the opposite in DestroyWindow, to make sure the internal handle is released:

BOOL CMyDialog::DestroyWindow()
{
    // Take Acrobat out of modal dialog mode, and release our HWND
    AVAppEndModal();
    AVWindowDestroy(m_AVdlgWin);
    return CDialog::DestroyWindow();
}

This example assumes CMyDialog is always modal.

If you fail to release a handle obtained by GetSafeHwnd, that's when you get the assertion failure. What exactly releasing a handle means depends on what you did with it. One can only guess.