“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。

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

 

posted @ 2013-12-01 15:28  xingrun  阅读(565)  评论(0编辑  收藏  举报