您的位置:首页 > 大数据 > 人工智能

VC6.0常见错误之::Debug Assertion Failed!

2013-08-27 11:20 295 查看
 


VC6.0常见错误之::Debug Assertion Failed!

1、VC6.0常见错误之:Debug Assertion Failed!在winocc.cpp第301行错误

断言错误,如图:



找到winocc中的源代码如下:

[cpp] view
plaincopy

BOOL CWnd::ShowWindow(int nCmdShow)  

{  

 ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));//错误在此  

  

 if (m_pCtrlSite == NULL)  

  return ::ShowWindow(m_hWnd, nCmdShow);  

 else  

  return m_pCtrlSite->ShowWindow(nCmdShow);  

}  

错误原因分析:

下面是我原来的写法,导致错误:

[cpp] view
plaincopy

BOOL CDlgControlPanel::HideAll()  

{  

    this->m_pDlgReport = new CDlgReport;  

    this->m_pDlgReport->Create(CDlgReport::IDD,this);  

    this->m_pDlgReport->ShowWindow(SW_SHOW);  

}  

解决方法:

[cpp] view
plaincopy

BOOL CDlgControlPanel::HideAll()  

{  

    this->m_pDlgReport = new CDlgReport;  

    this->m_pDlgReport->Create(CDlgReport::IDD,this);  

    ::ShowWindow(this->m_pDlgReport->m_hWnd, SW_SHOW);  

}  

区别就在于调用ShowWindow的方式。

真正原因是什么呢,是因为API传进去的句柄是这个控件的句柄,即控件->m_hWnd,而MFC传进去的句柄是此类(即CDlgReport类)的句柄,即this->m_hWnd。还有一点需要注意的是,可能在Release版本有时会运行正常,就侥幸这样用了,其实不管是Release版本还是Debug版本,都必须用上面的办法处理,如果你要控制某一个控件,那么你就传它对应的句柄,不要传别人的句柄进去。

感谢“C++帮助文档”的博客:http://hi.baidu.com/vc_net/blog/item/0fc01f307812d580a8018e2c.html

2、VC6.0常见错误之:Debug Assertion Failed!在dlgdata.cpp第43行错误

对话框中某个ID对应的控件不存在 

估计删除了界面上的控件,但是没有删除其映射关系 

以下是出现断言错误的地方

[cpp] view
plaincopy

HWND   CDataExchange::PrepareCtrl(int   nIDC)   

{   

ASSERT(nIDC   !=   0);   

ASSERT(nIDC   !=   -1);   //   not   allowed   

HWND   hWndCtrl;   

m_pDlgWnd-> GetDlgItem(nIDC,   &hWndCtrl);   

if   (hWndCtrl   ==   NULL)   

{   

TRACE1( "Error:   no   data   exchange   control   with   ID   0x%04X.\n ",   nIDC);   

ASSERT(FALSE);  //错误在此  

  

AfxThrowNotSupportedException();   

}   

m_hWndLastControl   =   hWndCtrl;   

m_bEditLastControl   =   FALSE;   //   not   an   edit   item   by   default   

ASSERT(hWndCtrl   !=   NULL);       //   never   return   NULL   handle   

return   hWndCtrl;   

}  

感谢vocanicy,来自http://topic.csdn.net/u/20070919/18/98a3444e-ff9b-4f7b-acb5-a35783379376.html

3、VC6.0常见错误之:Debug Assertion Failed!在winctrl1.cpp在184行

错误源代码如下:

[cpp] view
plaincopy

// Derived class is responsible for implementing these handlers  

//   for owner/self draw controls (except for the optional DeleteItem)  

void CComboBox::DrawItem(LPDRAWITEMSTRUCT)  

{ ASSERT(FALSE); }//错误在此  

void CComboBox::MeasureItem(LPMEASUREITEMSTRUCT)  

{ ASSERT(FALSE); }  

int CComboBox::CompareItem(LPCOMPAREITEMSTRUCT)  

{ ASSERT(FALSE); return 0; }  

void CComboBox::DeleteItem(LPDELETEITEMSTRUCT)  

{ /* default to nothing */ }  

 

错误指向CComboBox控件不能绘制。当时我遇到的问题原因是,我把这个控件当做自定义MyComboBox类使用,但在声明的时候忘了修改类型,导致报错。

解决方法只要在头文件中修改控件类型就行了。

其实关于winctrl1.cpp,很多类似的错误也一样的。都是控件类型搞错。

4、VC6.0常见错误之:afxwin2.inl第126行

错误源代码如下:

[cpp] view
plaincopy

_AFXWIN_INLINE void CWnd::Invalidate(BOOL bErase)  

{  

 ASSERT(::IsWindow(m_hWnd));\\错误在此  

 ::InvalidateRect(m_hWnd, NULL, bErase);  

 }  

看到IsWindow就明白了吧,是空句柄!也就是该对话框还未创建。

我遇到的情况是,A类的Clear函数中调用了this->Invalidate();,而B类调用了A类的Clear函数,此时A类还未实例化,所以A类句柄为空,m_hWnd = NULL。我的解决方法是将A类的实例化放在B类前,问题解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: