I am using _set_invalid_parameter_handler to override the default behaviour of the program when a CRT function gets an invalid parameter, which is to crash with 0xc0000417 (STATUS_INVALID_CRUNTIME_PARAMETER).
This is my handler:
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);
}
I want to log the information and send an error report. In the Debug build I get useful information with the parameters, but in the Release build all the parameters are NULL, which is not very useful. Is there any way to add useful information in the Release builds as well?
It is explicitly mentioned in the Remarks section of the MSDN Library article:
The parameters all have the value NULL unless a debug version of the CRT library is used
The reason is visible from the crtdefs.h header file, edited for readability:
#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 */
One optimization too many, I'd say. Being able to #define _CRT_SECURE_INVALID_PARAMETER yourself looks appealing but does not work unless you rebuild the CRT yourself. That's not exactly practical.