zoukankan      html  css  js  c++  java
  • “Debug Assertion” Runtime Error on VS2008 VS2010 winhand.cpp

    I'm writing a C++ MFC program on VS2008 and I'm getting this "Debug Assertion Error" when I first run the program sometimes. When I try to debug it, it takes me to this winhand.cpp file which is not part of the program I wrote so I'm not sure how to debug this.

    It takes the error to this place in winhand.cpp

     CObject* pTemp = LookupTemporary(h);
     if (pTemp != NULL)
     {
      // temporary objects must have correct handle values
      HANDLE* ph = (HANDLE*)((BYTE*)pTemp + m_nOffset);  // after CObject
      ASSERT(ph[0] == h || ph[0] == NULL);
      if (m_nHandles == 2)
       ASSERT(ph[1] == h);
     }
    

      

    So why does this error happen? Why does it only happen sometimes (50% of the time)? How would I debug this?

    I'll provide some code if is needed.

    THANKS!

    Answers

    The code that is asserting is part of MFC's CHandleMap class. MFC deals with windows as CWndobjects, but Windows deals with them as HWND handles. the handle map allows MFC to 'convert' anHWND into a pointer to the MFC object representing that object.

    What the assertion seems to be doing is checking that when a lookup of the handle finds an MFC object, that the MFC object also thinks it's wrapping the same handle.

    If they're different, then you get the assertion.

    So it would appear that something is corrupting the handle map or the MFC object for that handle or you're doing something incorrect that gets these 2 data structures out of sync.

    Some things you might do to try to debug the problem is to determine:

    • what MFC object is being found in the lookup (that's what's being pointed to by pObject)
    • what the MFC object thinks it's wrapping (that's the handle ph[0] and/or ph[1] - I'm not sure why there can be 2 of them)
    • what the handle is for (that's h)

    Do the handles look like handle values or do they look like garbage? Does pObject point to something that looks like an MFC object, or garbage? Do any of these these things seem related?

    The answers to these questions may point to what you need to do next (maybe set a debug write breakpoint on the item that looks like it's trashed).

    我在coding的时候也遇到这个问题了,后来找到了出错语句:

    ((COutPutDlg*)GetParent())->m_CurIndex=m_CurIndex;
    

      上面语句所在的类CPreviewDlg的对象窗体本来处于类COutPutDlg对象窗体之上,即前者是后者的子窗体,后来我使用语句:

    SetParent(GetDesktopWindow()); 

    该语句改变了CPreviewDlg对象的父窗体,改变后,调用((COutPutDlg*)GetParent())->m_CurIndex=m_CurIndex;并不会报错,但是在程序退出的时候,就出现了“Debug Assertion” Runtime Error。

    原因好像就是上文中说的对应关系被破坏了,不是很了解,还有其他事,暂且放一放。

  • 相关阅读:
    微信小程序使用canvas画布实现当前页面截屏并分享
    微信小程序分享转发用法大全——自定义分享、全局分享、组合分享
    小程序条形码插件wxbarcode的使用及改进
    支付宝小程序开发——修改小程序原生radio默认样式
    常用maven配置
    Android Studio 星云常用配置工具箱
    星云最佳实践功法秘籍
    Intellij Idea 星云常用配置工具箱
    那些好用的Chrome 插件
    星云的Linux专用学习手册
  • 原文地址:https://www.cnblogs.com/xingrun/p/3452642.html
Copyright © 2011-2022 走看看