_invalid_parameter在发布版本中没有获得有用的信息

时间:2012-03-10 15:47:37

标签: c visual-studio-2008 error-handling crt

当CRT函数获取无效参数时,我使用_set_invalid_parameter_handler来覆盖程序的默认行为,该参数将与0xc0000417(STATUS_INVALID_CRUNTIME_PARAMETER)崩溃。

这是我的经纪人:

void my_invalid_parameter_handler(
    const wchar_t * expression,
    const wchar_t * function, 
    const wchar_t * file, 
    unsigned int line,
    uintptr_t pReserved
    )
{
    Log(L"Invalid parameter detected");
    Log(L"expression= %s", expression);
    Log(L"function= %s", function);
    Log(L"file= %s", file);
    Log(L"line= %d", line);
    Log(L"pReserved= %p", pReserved);
}

我想记录信息并发送错误报告。在Debug构建中,我获得了有关参数的有用信息,但在Release构建中,所有参数都是NULL,这不是很有用。有没有办法在Release版本中添加有用的信息?

1 个答案:

答案 0 :(得分:4)

MSDN Library article的备注部分明确提到:

  

除非使用CRT库的调试版本,否则参数都具有NULL值

原因可从crtdefs.h头文件中看到,为便于阅读而编辑:

#ifdef _DEBUG
#  ifndef _CRT_SECURE_INVALID_PARAMETER
#    define _CRT_SECURE_INVALID_PARAMETER(expr) \
       ::_invalid_parameter(__STR2WSTR(#expr), _FUNCTIONW__, __FILEW__, __LINE__, 0)
#  endif

#else

 /* By default, _CRT_SECURE_INVALID_PARAMETER in retail invokes_invalid_parameter_noinfo_noreturn(),
  * which is marked __declspec(noreturn) and does not return control to the application. Even if
  * _set_invalid_parameter_handler() is used to set a new invalid parameter handler which does return
  * control to the application, _invalid_parameter_noinfo_noreturn() will terminate the application and
  * invoke Watson. You can overwrite the definition of _CRT_SECURE_INVALID_PARAMETER if you need.
  *
  * _CRT_SECURE_INVALID_PARAMETER is used in the Standard C++ Libraries and the SafeInt library.
  */
#  ifndef _CRT_SECURE_INVALID_PARAMETER
#    define _CRT_SECURE_INVALID_PARAMETER(expr) ::_invalid_parameter_noinfo_noreturn()
#  endif  /* _CRT_SECURE_INVALID_PARAMETER */
#endif  /* _DEBUG */

一个优化太多了,我会说。能够自己#define _CRT_SECURE_INVALID_PARAMETER看起来很吸引人,但除非你自己重建CRT,否则它不起作用。这不太准确。