太牛逼了,也可看大牛原帖:http://blog.chinaunix.net/uid-24862988-id-3793502.html
1、問題
在修改單線程MFC程序爲多線程時,遇到了CWnd::AssertValid()函數執行出錯問題。主要表現是在執行下面代碼中綠色語句時出錯
<span style="color:#666666;">#ifdef _DEBUG
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();<span style="white-space:pre"> </span>//此處問題
ASSERT(pMap != NULL);<span style="white-space:pre"> </span>//此處問題
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.
}
}
#endif</span>
2、原因
通過在網上查詢資料發現問題的原因:在B線程中,直接通過A線程的窗口實例指針調用窗口關聯函數。在MFC中,窗口類的事件映射表是和線程相關聯,故只有在本線程中才能通過窗口實例指針調用該窗口關聯函數。下面這段代碼是afxMapHWND函數的實現,其中綠色部分是線程相關的變量的結構體的獲取,事件映射表就是這個結構體的成員變量
CHandleMap* PASCAL afxMapHWND(BOOL bCreate)
{
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
if (pState->m_pmapHWND == NULL && bCreate)
{
BOOL bEnable = AfxEnableMemoryTracking(FALSE);
#ifndef _AFX_PORTABLE
_PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
#endif
pState->m_pmapHWND = new CHandleMap(RUNTIME_CLASS(CWnd),
ConstructDestruct<CWnd>::Construct, ConstructDestruct<CWnd>::Destruct,
offsetof(CWnd, m_hWnd));
#ifndef _AFX_PORTABLE
AfxSetNewHandler(pnhOldHandler);
#endif
AfxEnableMemoryTracking(bEnable);
}
return pState->m_pmapHWND;
}
3、方法
要徹底解決這個問題,那麼就需要通過多線程的線程間消息機制解決上述問題,也就是自定義消息,在子線程中需要調用主線程create的時候,向主線程發消息,主線程消息響應函數中去create