太牛逼了,也可看大牛原帖: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