VinceYuan

Personal Blog

导航

在Debug目标代码中禁止ASSERT窗口出现

Posted on 2005-01-06 15:33  Vince Yuan  阅读(3283)  评论(3编辑  收藏  举报

作者 VinceYuan

版本 v1.2
在Visual studio .net 2002/2003测试通过
 

 

ASSERT()是个很有用的东西,可以帮助我们调试程序。我们会像下面这样使用ASSERT()

ASSERT( i == 1 );

 目的是保证i的值为1。如果i != 1 (也就是ASSERT的参数为FALSE),会自动出现一个下面这个窗口,告诉我们程序出问题了。我们可以选择Ignore跳过这个ASSERT。(当然在Release目标代码中,ASSERT会被自动清除掉,永远不会出现ASSERT窗口。)

 

 

可是当软件规模非常大的时候,会遇到很多个ASSERT窗口,这个时候一个一个“Ignore”是非常令人头疼的一件事情。换到Release版,当然就没有了这些讨厌的窗口,可是又不能调试了。
 

在Debug目标代码中禁止ASSERT窗口出现的方法如下:


Visual Studio .net 2002Watch窗口中输入

{,,msvcr70d.dll}_CrtSetReportMode(2,2)

如果你使用的是Visual Studio.net 2003,请输入

{,,msvcr71d.dll}_CrtSetReportMode(2,2)

 设置完之后,你会发现ASSERT窗口没有了,只在Output窗口有文字输出。

注意:

  1. 只有设置了断点并且中止了程序,Watch窗口才允许你输入。
  2. 此命令的效果是全局性的。
  3. 若想重新出现ASSERT窗口,只须在Visual Studio .net 2002Watch窗口中输入
    {,,msvcr70d.dll}_CrtSetReportMode(2,6)

    如果使用Visual Studio .net 2003,请输入
    {,,msvcr71d.dll}_CrtSetReportMode(2,6)

 MSDN中有很多重要说明,请参见_CrtSetReportMode
摘录部分内容如下:

_CrtSetReportMode specifies the output destination for _CrtDbgReport. Because the macros _ASSERT, _ASSERTE, _RPT, and _RPTF call _CrtDbgReport, _CrtSetReportMode specifies the output destination of text specified with those macros.

When _DEBUG is not defined, calls to _CrtSetReportMode are removed during preprocessing.

If you do not call _CrtSetReportMode to define the output destination of messages, the following defaults are in effect:

  • Assertion failures and errors are directed to a debug message window.
  • Warnings from Windows applications are sent to the debugger's output window.
  • Warnings from console applications are not displayed.

The following table lists the report types defined in CRTDBG.H.

Report type

Description

_CRT_WARN

Warnings, messages, and information that does not need immediate attention.

_CRT_ERROR

Errors, unrecoverable problems, and issues that require immediate attention.

_CRT_ASSERT

Assertion failures (asserted expressions that evaluate to FALSE).

The _CrtSetReportMode function assigns the new report mode specified in reportMode to the report type specified in reportType and returns the previously defined report mode for reportType. The following table lists the available choices for reportMode and the resulting behavior of _CrtDbgReport. These options are defined as bit-flags in CRTDBG.H.

Report mode

_CrtDbgReport behavior

_CRTDBG_MODE_DEBUG

Writes the message to the debugger's output window.

_CRTDBG_MODE_FILE

Writes the message to a user-supplied file handle. _CrtSetReportFile should be called to define the specific file or stream to use as the destination.

_CRTDBG_MODE_WNDW

Creates a message box to display the message along with the Abort, Retry, and Ignore buttons.

_CRTDBG_REPORT_MODE

Returns reportMode for the specified reportType:

1   _CRTDBG_MODE_FILE

2   _CRTDBG_MODE_DEBUG

4   _CRTDBG_MODE_WNDW

Each report type can be reported using one, two, or three modes, or no mode at all. Therefore, it is possible to have more than one destination defined for a single report type. For example, the following code fragment causes assertion failures to be sent to both a debug message window and to stderr:

_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );

In addition, the reporting mode or modes for each report type can be separately controlled. For example, it is possible to specify that a reportType of _CRT_WARN be sent to an output debug string, while _CRT_ASSERT be displayed using a debug message window and sent to stderr, as previously illustrated.