CWnd-AssertVaild


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??? values
	ASSERT(HWND_TOP == NULL);       // same as desktop
	if (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 window
		ASSERT(::IsWindow(m_hWnd));

		// should also be in the permanent or temporary handle map
		CHandleMap* 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 _DEBUG
    	virtual 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;
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章