ASSERT((CWnd*)p == this); // must be us

来源:互联网 发布:js实现日期格式化 编辑:程序博客网 时间:2024/06/06 00:53

转载自http://blog.chinaunix.net/uid-24862988-id-3793502.html

今天使用一个动态库时,调用动态库的创建窗口函数出现assert错误,

BOOL CADASISCSDlg::TMUI_Init(VOID* pParam, CWnd* pParent/*=NULL*/)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());


Create(IDD_ADASISCS_DIALOG, pParent);//出错语句
return TRUE;
}

原因应该是在不同线程间传递窗口句柄,按下面的文章修改后变成功通过。

BOOL CADASISCSDlg::TMUI_Init(VOID* pParam, CWnd* pParent/*=NULL*/)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());


Create(IDD_ADASISCS_DIALOG, pParent->FromHandle(pParent->GetSafeHwnd()));//修改后
return TRUE;
}


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( (= pMap->LookupPermanent(m_hWnd)) != NULL ||
  25.                     (= pMap->LookupTemporary(m_hWnd)) != NULL);
  26.         }
  27.         ASSERT((CWnd*)== 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. }



点击(此处)折叠或打开




    对标题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);

 



阅读全文
0 0
原创粉丝点击