1. 问题
在修改单线程MFC程序为多线程时,遇到了CWnd::AssertValid()函数执行出错问题。主要表现是在执行下面代码中绿色语句时出错
-
#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();
-
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.
-
}
-
}
-
#endif
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;
-
}
下面的一段代码是函数定义和函数调用。CMainFrame类的实例指针mainFrame是主线程(A线程)的窗口实例指针,该指针在工作线程(B线程)中被用来调用类的成员函数getHtmlView,该函数是窗口关联函数(获取窗口中的视窗)。当在工作线程中获取窗口视窗时,就会调用函数CWnd::AssertValid(),执行该函数时会调用上面的afxMapHWND函数。很显然上述代码中绿色中的结构体AFX_MODULE_THREAD_STATE
获取的是当前线程(即工作线程)的结构体,而非主线程的结构体,故出现了标题1中描述的问题
// 在B线程中执行下述代码
-
CMainFrame *mainFrame = (CMainFrame *)AfxGetApp()->GetMainWnd();
-
utHtmlView *htmlView = (utHtmlView *)mainFrame->getHtmlView();
// 函数定义
-
CView *CMainFrame::getHtmlView( void )
-
{
-
if(!m_nMultiInterface)
-
return GetActiveView();
-
return (CView *)m_wndSplitter.GetPane(0, 1);
-
}
3. 解决
1) 方法一
CWnd::AssertValid只会在Debug下有效,而上述问题只会在多线程程序中出现。简单的解决方法是在Release下运行程序,就可以避免上述问题
2)方法二
用 FromHandle 返回临时对象的指针,就可以调用各种类的方法。临时对象会在 OnIdle 中销毁。这里对 FromHandle 的实现原理从源码上进行解析
下面代码是 CWnd 的 FromHandle 方法,大致的意思为从 CHandleMap 中获取临时 CWnd 对象的指针
-
CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
-
{
-
CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist
-
ASSERT(pMap != NULL);
-
CWnd* pWnd = (CWnd*)pMap->FromHandle(hWnd);
-
#ifndef _AFX_NO_OCC_SUPPORT
-
pWnd->AttachControlSite(pMap);
-
#endif
-
ASSERT(pWnd == NULL || pWnd->m_hWnd == hWnd);
-
return pWnd;
-
}
-
//
-
// 2
-
//
-
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(CTempWnd),
-
offsetof(CWnd, m_hWnd));
-
#ifndef _AFX_PORTABLE
-
AfxSetNewHandler(pnhOldHandler);
-
#endif
-
AfxEnableMemoryTracking(bEnable);
-
}
-
return pState->m_pmapHWND;
-
}
-
-
再看
-
-
#define offsetof(s,m) (size_t)&(((s *)0)->m)
-
-
继续
-
-
//
-
// 3
-
//
-
CObject* CHandleMap::FromHandle(HANDLE h)
-
{
-
ASSERT(m_pClass != NULL);
-
ASSERT(m_nHandles == 1 || m_nHandles == 2);
-
-
if (h == NULL)
-
return NULL;
-
-
CObject* pObject = LookupPermanent(h);
-
if (pObject != NULL)
-
return pObject; // return permanent one
-
else if ((pObject = LookupTemporary(h)) != NULL)
-
{
-
HANDLE* ph = (HANDLE*)((BYTE*)pObject + m_nOffset);
-
ASSERT(ph[0] == h || ph[0] == NULL);
-
ph[0] = h;
-
if (m_nHandles == 2)
-
{
-
ASSERT(ph[1] == h || ph[1] == NULL);
-
ph[1] = h;
-
}
-
return pObject; // return current temporary one
-
}
-
-
// This handle wasn't created by us, so we must create a temporary
-
// C++ object to wrap it. We don't want the user to see this memory
-
// allocation, so we turn tracing off.
-
-
BOOL bEnable = AfxEnableMemoryTracking(FALSE);
-
#ifndef _AFX_PORTABLE
-
_PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
-
#endif
-
-
CObject* pTemp = NULL;
-
TRY
-
{
-
pTemp = m_pClass->CreateObject();
-
if (pTemp == NULL)
-
AfxThrowMemoryException();
-
-
m_temporaryMap.SetAt((LPVOID)h, pTemp);
-
}
-
CATCH_ALL(e)
-
{
-
#ifndef _AFX_PORTABLE
-
AfxSetNewHandler(pnhOldHandler);
-
#endif
-
AfxEnableMemoryTracking(bEnable);
-
THROW_LAST();
-
}
-
END_CATCH_ALL
-
-
#ifndef _AFX_PORTABLE
-
AfxSetNewHandler(pnhOldHandler);
-
#endif
-
AfxEnableMemoryTracking(bEnable);
-
-
// now set the handle in the object
-
HANDLE* ph = (HANDLE*)((BYTE*)pTemp + m_nOffset); // after CObject
-
ph[0] = h;
-
if (m_nHandles == 2)
-
ph[1] = h;
-
-
return pTemp;
-
}
对标题2中的程序进行修改如下,就可以避免出现上面的问题。首先获取到主窗口实例指针,然后根据主窗口实例指针的窗口句柄获取到临时主窗口实例指针,最后就可以通过临时实例指针操作该类中的各种方法了
-
CWnd *mWnd = AfxGetApp()->GetMainWnd();
-
CMainFrame *mainFrame = (CMainFrame *)(mWnd->FromHandle(mWnd->GetSafeHwnd()));
-
utHtmlView *htmlView = (utHtmlView *)mainFrame->getHtmlView();
3)方法三
方法二虽然解决了上面遇到的问题,但是不能完全解决。比如在主窗口下还有子窗口,通过上述方法主窗口可以调用类的各种方法,但是通过类方法再去调用子窗口的窗口关联函数时还是会出现问题。比如在下面的代码中,在主窗口的方法中,调用子窗口的窗口方法还是会遇到类似问题
-
CView *CMainFrame::getHtmlView( void )
-
{
-
if(!m_nMultiInterface)
-
return GetActiveView();
-
return (CView *)m_wndSplitter.GetPane(0, 1);
-
}
要彻底解决这个问题,那么就需要通过多线程的线程间消息机制解决上述问题,MFC多线程消息机制
///////////////////////////////////////////////////////////////////////////////////////////////////////////
A. 声明与定义
(1) 消息声明
#define WM_MY_MESSAGE WM_USER + 100
(2) 类声明中添加以下声明
DECLARE_MESSAGE_MAP()
afx_msg LRESULT OnMyMessage(WPARAM wParam,LPARAM lParam);
(3) 类实现中添加下一代码
BEGIN_MESSAGE_MAP(MYCLASS, MYPARENTCLASS)
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
END_MESSAGE_MAP()
LRESULT MYCLASS::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
return S_OK;
}
////////////////////////////////////////////////////////////////////////////
B. 工作线程代码实现
MYCLASS *myClass;
同步消息传递
HRESULT hr = myClass->SendMessage(WM_MY_MESSAGE, wParam, lParam);
异步消息传递
HRESULT hr = myClass->PostMessage(WM_MY_MESSAGE, wParam, lParam);
阅读(7905) | 评论(0) | 转发(0) |