如何在发布模式下获得类似调试的assert()? [英] How can I get a assert() like debug in release mode?

查看:82
本文介绍了如何在发布模式下获得类似调试的assert()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在调试模式下执行assert()行为,

任何建议?

(No-MFC)



我试图取消定义Undebug宏,但断言(0)将导致程序直接返回。

我希望它在调试模式下同样运行。

I want a assert() act like that in debug mode,
any advice?
(No-MFC)

I have tried to undefine the Undebug macro,but the assert(0) will cause program directly return.
I want it to act same with in debug mode.

推荐答案

您可以编写自己的:

You can write your own:
#define MY_ASSERT(f) (void) ((f) || !MyAssertFailedLine(__FILE__, __LINE__))

BOOL MyAssertFailedLine(LPCSTR lpszFile, int nLine)
{
    char lpszMsg[512];
    _snprintf(lpszMsg, 512, "MyAssert failed in %s on line %d.\nExit?", 
        lpszFile, nLine);
    lpszMsg[511] = '\0';
    if (IDYES == ::MessageBoxA(NULL, lpszMsg, NULL, MB_YESNO))
        ::PostQuitMessage(1);
    return TRUE;
}



如果要替换标准的 ASSERT()宏,可以重新定义它:


If you want to replace the standard ASSERT() macro, you can redefine it:

#ifndef _DEBUG
#undef ASSERT
#define ASSERT(f) (void) ((f) || !MyAssertFailedLine(__FILE__, __LINE__))
#endif


这篇关于如何在发布模式下获得类似调试的assert()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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