Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Customizing Native Run-Time Checks

When you compile with /RTC (run-time checks) or use the runtime_checks pragma, the C run-time library provides native run-time checks. In some cases, you may want to customize run-time checking:

  • To route run-time check messages to a file or destination other than the default.
  • To specify an output destination for run-time check messages under a third-party debugger.
  • To report run-time check messages from a program compiled with a release version of the C run-time library. (Release versions of the library do not use _CrtDbgReport to report run-time errors. Instead, they display an Assert dialog box for each run-time error.) For more information, see

To customize run-time error checking, you can:

Writing a Run-Time Error Reporting Function

A custom reporting function for run-time errors must have same declaration as _CrtDbgReport. It should return value of 1 to the debugger.

The following example shows how to define a custom reporting function:

int errorhandler = 0;
void configureMyErrorFunc(int i)
{
   errorhandler = i;
}

int MyErrorFunc(int errorType, const char *filename, int linenumber, const char *moduleName, const char *format, ...)
{
   switch (errorhandler)
   {
   case 0:
   case 1:
      printf("Error type %d at %s line %d in %s",errorType. filename, linenumber, moduleName);
   case 2:
   case 3:
      fprintf(stderr, "Error type);
   }
}

The following example shows a more complex custom reporting function. In this example, the switch statement handles various error types, as defined by the reportType parameter of _CrtDbgReport. Because you are replacing _CrtDbgReport, you cannot use _CrtSetReportMode. Your function must handle the output. The first variable argument in this function takes a run-time error number. For more information, see _RTC_SetErrorType.

#include <windows.h>
#include <stdarg.h>
#pragma runtime_checks("", off)
int Catch_RTC_Failure(int errType, const char *file, int line, 
                      const char *module, const char *format, ...)
{
   // Prevent re-entrancy
   static long running = 0;
   while (InterlockedExchange(&running, 1))
      Sleep(0);
   // Now, disable all RTC failures
   int numErrors = _RTC_GetNumErrors();
   int *errors=_alloca(numErrors);
   for (int i = 0; i < numErrors; i++)
   errors[i] = _RTC_SetErrorType(i, _RTC_ERRTYPE_IGNORE);

   // First, let's get the rtc error number from the var-arg list...
   va_list vl;
   va_start(vl, format);
   _RTC_ErrorNumber rtc_errnum = va_arg(vl, _RTC_ErrorNumber);
   va_end(vl);

   char buf[512];
   const char *err = _RTC_GetErrDesc(rtc_errnum);
   sprintf(buf, "%s\nLine #%d\nFile:%s\nModule:%s",
      err,
      line,
      file ? file : "Unknown",
      module ? module : "Unknown");
   int res = (MessageBox(NULL, buf, "RTC Failed...", MB_YESNO) == IDYES) ? 1 : 0;
   // Now, restore the RTC errortypes
   for(i = 0; i < numErrors; i++)
      _RTC_SetErrorType(i, errors[i]);
   running = 0;
   return res;
}
#pragma runtime_checks("", restore)

Use _RTC_SetErrorFunc to install your custom function in place of _CrtDbgReport. For more information, see _RTC_SetErrorFunc. The _RTC_SetErrorFunc return value is the previous reporting function, which you can save and restore if necessary:

int main()
{
   _RTC_error_fn oldfunction, newfunc;
   oldfunction = _RTC_SetErrorFunc(&MyErrorFunc);
   // run some code...
   newfunc = _RTC_SetErrorFunc(oldfunction);
   // newfunc == &MyErrorFunc;
   // run some more code...
}

Customizing the Error Message Destination

If you use _CrtDbgReport to report errors, you can use _CrtSetReportMode to specify the destination of error messages.

If you use a custom reporting function, use _RTC_SetErrorType to associate an error with a report type.

Querying for Information About Run-Time Checks

_RTC_NumErrors returns the number of error types detect by run-time error checks. To get a brief description of each error, you can loop from 0 to (_RTC_NumErrors –1) return value, passing the iteration value to _RTC_GetErrDesc on each loop. For more information, see _RTC_NumErrors and _RTC_GetErrDesc.

See Also

Run-Time Error Checks | runtime_checks | _CrtDbgReport

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.