Chinaunix首页 | 论坛 | 博客
  • 博客访问: 639544
  • 博文数量: 133
  • 博客积分: 1566
  • 博客等级: 上尉
  • 技术积分: 1230
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-01 09:31
文章分类

全部博文(133)

文章存档

2019年(1)

2018年(1)

2017年(8)

2016年(9)

2015年(17)

2014年(4)

2013年(31)

2012年(25)

2011年(36)

2010年(1)

我的朋友

分类: Windows平台

2013-07-09 10:28:52

1. 问题

    在修改单线程MFC程序为多线程时,遇到了CWnd::AssertValid()函数执行出错问题。主要表现是在执行下面代码中绿色语句时出错

点击(此处)折叠或打开

  1. #ifdef _DEBUG
  2. void CWnd::AssertValid() const
  3. {
  4.     if (m_hWnd == NULL)
  5.         return; // null (unattached) windows are valid

  6.     // check for special wnd??? values
  7.     ASSERT(HWND_TOP == NULL); // same as desktop
  8.     if (m_hWnd == HWND_BOTTOM)
  9.         ASSERT(this == &CWnd::wndBottom);
  10.     else if (m_hWnd == HWND_TOPMOST)
  11.         ASSERT(this == &CWnd::wndTopMost);
  12.     else if (m_hWnd == HWND_NOTOPMOST)
  13.         ASSERT(this == &CWnd::wndNoTopMost);
  14.     else
  15.     {
  16.         // should be a normal window
  17.         ASSERT(::IsWindow(m_hWnd));

  18.         // should also be in the permanent or temporary handle map
  19.         CHandleMap* pMap = afxMapHWND();
  20.         ASSERT(pMap != NULL);

  21.         CObject* p=NULL;
  22.         if(pMap)
  23.         {
  24.             ASSERT( (p = pMap->LookupPermanent(m_hWnd)) != NULL ||
  25.                     (p = pMap->LookupTemporary(m_hWnd)) != NULL);
  26.         }
  27.         ASSERT((CWnd*)p == this); // must be us

  28.         // Note: if either of the above asserts fire and you are
  29.         // writing a multithreaded application, it is likely that
  30.         // you have passed a C++ object from one thread to another
  31.         // and have used that object in a way that was not intended.
  32.         // (only simple inline wrapper functions should be used)
  33.         //
  34.         // In general, CWnd objects should be passed by HWND from
  35.         // one thread to another. The receiving thread can wrap
  36.         // the HWND with a CWnd object by using CWnd::FromHandle.
  37.         //
  38.         // It is dangerous to pass C++ objects from one thread to
  39.         // another, unless the objects are designed to be used in
  40.         // such a manner.
  41.     }
  42. }
  43. #endif


2. 原因

    通过在网上查询资料发现问题的原因:在B线程中,直接通过A线程的窗口实例指针调用窗口关联函数。在MFC中,窗口类的事件映射表是和线程相关联,故只有在本线程中才能通过窗口实例指针调用该窗口关联函数。下面这段代码是afxMapHWND函数的实现,其中绿色部分是线程相关的变量的结构体的获取,事件映射表就是这个结构体的成员变量

点击(此处)折叠或打开

  1. CHandleMap* PASCAL afxMapHWND(BOOL bCreate)
  2. {
  3.     AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  4.     if (pState->m_pmapHWND == NULL && bCreate)
  5.     {
  6.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  7. #ifndef _AFX_PORTABLE
  8.         _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  9. #endif
  10.         pState->m_pmapHWND = new CHandleMap(RUNTIME_CLASS(CWnd),
  11.             ConstructDestruct<CWnd>::Construct, ConstructDestruct<CWnd>::Destruct,
  12.             offsetof(CWnd, m_hWnd));

  13. #ifndef _AFX_PORTABLE
  14.         AfxSetNewHandler(pnhOldHandler);
  15. #endif
  16.         AfxEnableMemoryTracking(bEnable);
  17.     }
  18.     return pState->m_pmapHWND;
  19. }
    
    下面的一段代码是函数定义和函数调用。CMainFrame类的实例指针mainFrame是主线程(A线程)的窗口实例指针,该指针在工作线程(B线程)中被用来调用类的成员函数getHtmlView,该函数是窗口关联函数(获取窗口中的视窗)。当在工作线程中获取窗口视窗时,就会调用函数CWnd::AssertValid(),执行该函数时会调用上面的afxMapHWND函数。很显然上述代码中绿色中的结构体AFX_MODULE_THREAD_STATE获取的是当前线程(即工作线程)的结构体,而非主线程的结构体,故出现了标题1中描述的问题


点击(此处)折叠或打开

      // 在B线程中执行下述代码

  1. CMainFrame *mainFrame = (CMainFrame *)AfxGetApp()->GetMainWnd();
  2. utHtmlView *htmlView = (utHtmlView *)mainFrame->getHtmlView();


       // 函数定义

  1. CView *CMainFrame::getHtmlView( void )
  2. {
  3.  if(!m_nMultiInterface)
  4.   return GetActiveView();
  5.  return (CView *)m_wndSplitter.GetPane(0, 1);
  6. }

3. 解决

1) 方法一

    CWnd::AssertValid只会在Debug下有效,而上述问题只会在多线程程序中出现。简单的解决方法是在Release下运行程序,就可以避免上述问题

2)方法二

    用 FromHandle 返回临时对象的指针,就可以调用各种类的方法。临时对象会在 OnIdle 中销毁。这里对 FromHandle 的实现原理从源码上进行解析
下面代码是 CWnd 的 FromHandle 方法,大致的意思为从 CHandleMap 中获取临时 CWnd 对象的指针


点击(此处)折叠或打开

  1. CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
  2. {
  3.     CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist
  4.     ASSERT(pMap != NULL);
  5.     CWnd* pWnd = (CWnd*)pMap->FromHandle(hWnd);
  6. #ifndef _AFX_NO_OCC_SUPPORT
  7.     pWnd->AttachControlSite(pMap);
  8. #endif
  9.     ASSERT(pWnd == NULL || pWnd->m_hWnd == hWnd);
  10.     return pWnd;
  11. }



点击(此处)折叠或打开



  1. //
  2. // 2
  3. //
  4. CHandleMap* PASCAL afxMapHWND(BOOL bCreate)
  5. {
  6.     AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  7.     if (pState->m_pmapHWND == NULL && bCreate)
  8.     {
  9.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  10. #ifndef _AFX_PORTABLE
  11.         _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  12. #endif


  13.         pState->m_pmapHWND = new CHandleMap(RUNTIME_CLASS(CTempWnd),
  14.             offsetof(CWnd, m_hWnd));
  15. #ifndef _AFX_PORTABLE
  16.         AfxSetNewHandler(pnhOldHandler);
  17. #endif
  18.         AfxEnableMemoryTracking(bEnable);
  19.     }
  20.     return pState->m_pmapHWND;
  21. }

  22. 再看

  23. #define offsetof(s,m) (size_t)&(((s *)0)->m)

  24. 继续

  25. //
  26. // 3
  27. //
  28. CObject* CHandleMap::FromHandle(HANDLE h)
  29. {
  30.     ASSERT(m_pClass != NULL);
  31.     ASSERT(m_nHandles == 1 || m_nHandles == 2);

  32.     if (h == NULL)
  33.         return NULL;

  34.     CObject* pObject = LookupPermanent(h);
  35.     if (pObject != NULL)
  36.         return pObject; // return permanent one
  37.     else if ((pObject = LookupTemporary(h)) != NULL)
  38.     {
  39.         HANDLE* ph = (HANDLE*)((BYTE*)pObject + m_nOffset);
  40.         ASSERT(ph[0] == h || ph[0] == NULL);
  41.         ph[0] = h;
  42.         if (m_nHandles == 2)
  43.         {
  44.             ASSERT(ph[1] == h || ph[1] == NULL);
  45.             ph[1] = h;
  46.         }
  47.         return pObject; // return current temporary one
  48.     }

  49.     // This handle wasn't created by us, so we must create a temporary
  50.     // C++ object to wrap it. We don't want the user to see this memory
  51.     // allocation, so we turn tracing off.

  52.     BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  53. #ifndef _AFX_PORTABLE
  54.     _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  55. #endif

  56.     CObject* pTemp = NULL;
  57.     TRY
  58.     {
  59.         pTemp = m_pClass->CreateObject();
  60.         if (pTemp == NULL)
  61.             AfxThrowMemoryException();

  62.         m_temporaryMap.SetAt((LPVOID)h, pTemp);
  63.     }
  64.     CATCH_ALL(e)
  65.     {
  66. #ifndef _AFX_PORTABLE
  67.         AfxSetNewHandler(pnhOldHandler);
  68. #endif
  69.         AfxEnableMemoryTracking(bEnable);
  70.         THROW_LAST();
  71.     }
  72.     END_CATCH_ALL

  73. #ifndef _AFX_PORTABLE
  74.     AfxSetNewHandler(pnhOldHandler);
  75. #endif
  76.     AfxEnableMemoryTracking(bEnable);

  77.     // now set the handle in the object
  78.     HANDLE* ph = (HANDLE*)((BYTE*)pTemp + m_nOffset); // after CObject
  79.     ph[0] = h;
  80.     if (m_nHandles == 2)
  81.         ph[1] = h;

  82.     return pTemp;
  83. }


    对标题2中的程序进行修改如下,就可以避免出现上面的问题。首先获取到主窗口实例指针,然后根据主窗口实例指针的窗口句柄获取到临时主窗口实例指针,最后就可以通过临时实例指针操作该类中的各种方法了


点击(此处)折叠或打开

  1. CWnd *mWnd = AfxGetApp()->GetMainWnd();
  2. CMainFrame *mainFrame = (CMainFrame *)(mWnd->FromHandle(mWnd->GetSafeHwnd()));
  3. utHtmlView *htmlView = (utHtmlView *)mainFrame->getHtmlView();



3)方法三

    方法二虽然解决了上面遇到的问题,但是不能完全解决。比如在主窗口下还有子窗口,通过上述方法主窗口可以调用类的各种方法,但是通过类方法再去调用子窗口的窗口关联函数时还是会出现问题。比如在下面的代码中,在主窗口的方法中,调用子窗口的窗口方法还是会遇到类似问题

点击(此处)折叠或打开

  1. CView *CMainFrame::getHtmlView( void )
  2. {
  3.     if(!m_nMultiInterface)
  4.         return GetActiveView();
  5.     return (CView *)m_wndSplitter.GetPane(0, 1);
  6. }

    要彻底解决这个问题,那么就需要通过多线程的线程间消息机制解决上述问题,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) |
给主人留下些什么吧!~~