调试断言失败 [英] Debug assertion fail

查看:947
本文介绍了调试断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试程序.实际上,这是源代码,我为此设置了参数.我必须基于源代码编写一个应用程序.但是当我调试它时,出现了一条错误消息:
调试断言失败..."
请让我解决它.

I debug my program. in fact, it is a source code and I set arguments for it. I have to write a application base on the source code. But when I debug it, a error message had occured:
"debug assertion fail..."
Please have me solve it.

推荐答案

好吧,我认为第一件事是您正在使用MFC.

当某些不适当的内容时,大多数MFC代码都具有ASSERT宏,该宏会显示此对话框.例如,您编写一个函数StrLen,该函数接受一个字符指针:
Well, first thing I believe is that you are using MFC.

Most of the MFC''s code has ASSERT macros that show this dialog box, when something is not proper. For example, you write a function StrLen, which accepts a character-pointer:
int StrLen(const char* pString)
{
  // Iterate through string to find length
  // and return length
}

呼叫者是什么错误,例如:

what is caller makes a mistake like:

char* p = NULL;
StrLen(p);

在这种情况下,您的功能将导致访问冲突.好吧,您可以使用ASSERT来进行防御性编程,如下所示:

In this case, your function would cause access violation. Well, you can use defensive programming using ASSERT as follows:

int StrLen(const char* pString)
{
  ASSERT(pString != NULL); // Must not be null
  // Iterate through string to find length
  // and return length
}


在这种情况下,如果传递了null(非空字符串),则您的StrLen会针对null指针进行防御,这将导致调试断言失败"对话框.

SetWindowText这样的MFC方法很可能会为以下两个方面辩护:确保CWnd *(this指针是value,并且窗口本身是value window/control.这是通过以下方式实现的: />


In this case, if null (not empty string) is passed, your StrLen is defensing against null pointer, and this would cause "Debug Assertion Failed" dialog box.

An MFC method, like SetWindowText, would most probably defense itself for two concerns: Ensure the CWnd* (the this pointer is value, AND the window itself is value window/control. It is achieved through:

ASSERT(this != NULL);
ASSERT(::IsWindow(m_hWnd));

,并且如果任何 断言 失败,则会显示该对话框.只需单击重试",您将看到一个调用堆栈.找到错误并予以纠正!

and if any of assertion fails, you get that dialog box. Just click Retry and you will see a call stack. Find the bug and correct it!



这篇关于调试断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆