gpt4 book ai didi

c - _invalid_parameter 在发布版本中没有获取有用的信息

转载 作者:行者123 更新时间:2023-12-02 03:50:45 29 4
gpt4 key购买 nike

当 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);
}

我想记录信息并发送错误报告。在调试版本中,我通过参数获得有用的信息,但在发布版本中,所有参数都是 NULL,这不是很有用。有什么方法可以在发布版本中添加有用的信息吗?

最佳答案

MSDN Library article的备注部分明确提到了这一点:

The parameters all have the value NULL unless a debug version of the CRT library is used

原因可以从 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,否则不起作用。这不太实用。

关于c - _invalid_parameter 在发布版本中没有获取有用的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647753/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com