当前位置: 代码迷 >> 综合 >> CWnd-AssertVaild
  详细解决方案

CWnd-AssertVaild

热度:85   发布时间:2023-12-14 20:33:24.0

title: CWnd-AssertVaild
date: 2020-04-03 23:26:29
tags:

  • MFC
  • C++

  • Errror description

MFC dll中的界面在Debug时调用UpdateData()函数,会判断窗口的运行环境是否跨线程,如果创建窗口的线程与调用该函数的线程不是同一个,就会触发CWnd的AssertVaild()函数中断.提示:

it is likely that you have passed a C++ object from one thread to another and have used that object in a way that was not intended.把一个C++对象从一个线程传到另一个线程,使用对象的方式不正确(与代码的原本设计相悖)

void CWnd::AssertValid() const
{if (m_hWnd == NULL)return;     // null (unattached) windows are valid// check for special wnd??? valuesASSERT(HWND_TOP == NULL);       // same as desktopif (m_hWnd == HWND_BOTTOM)ASSERT(this == &CWnd::wndBottom);else if (m_hWnd == HWND_TOPMOST)ASSERT(this == &CWnd::wndTopMost);else if (m_hWnd == HWND_NOTOPMOST)ASSERT(this == &CWnd::wndNoTopMost);else{// should be a normal windowASSERT(::IsWindow(m_hWnd));// should also be in the permanent or temporary handle mapCHandleMap* pMap = afxMapHWND();ASSERT(pMap != NULL);CObject* p=NULL;if(pMap){ASSERT( (p = pMap->LookupPermanent(m_hWnd)) != NULL ||(p = pMap->LookupTemporary(m_hWnd)) != NULL);}ASSERT((CWnd*)p == this);   // must be us// Note: if either of the above asserts fire and you are// writing a multithreaded application, it is likely that// you have passed a C++ object from one thread to another// and have used that object in a way that was not intended.// (only simple inline wrapper functions should be used)//// In general, CWnd objects should be passed by HWND from// one thread to another.  The receiving thread can wrap// the HWND with a CWnd object by using CWnd::FromHandle.//// It is dangerous to pass C++ objects from one thread to// another, unless the objects are designed to be used in// such a manner.}
}
  • Solution

    1.在窗口或者控件类重载 CWnd::AssertValid() 函数,写个空函数屏蔽原函数,不推荐使用.

    class CExampleDialog: public CDialog{
    public:
    #ifdef _DEBUGvirtual void AssertValid() const {};
    #endif
    }
    

    只需要在Debug时重载,在release时不会出错,因为在CWnd中这个函数也是在Debug才生效

    #ifdef _DEBUG
    void CWnd::AssertValid() const
    {//....
    }
    void CWnd::Dump(CDumpContext& dc) const
    {//....
    }
    #endif
    

    2.这个错误主要是跨线程使用窗口的一些资源导致,在UpdateData()或其他资源使用前调用

    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    将状态切换.例如有如下函数需要在模块外调用且需要使用窗口/控件资源,

    bool ExampleFunc()
    {bool bRtn=true;AFX_MANAGE_STATE(AfxGetStaticModuleState());//切换线程状态  UpdateData(TRUE);//....return bRtn;
    }