warning C6031: Return value ignored: 'GetLastError'
来源:互联网 发布:java qq登录 编辑:程序博客网 时间:2024/05/01 21:45
#ifdef _M_CEE_PURE
#define GetLastError System::Runtime::InteropServices::Marshal::GetLastWin32Error
#else
WINBASEAPI
__checkReturn
DWORD
WINAPI
GetLastError(
VOID
);
#endif
返回参数有个 __checkReturn
查msdn发现:
warning C6031: return value ignored: <function> could return unexpected value
This warning indicates that the calling function is not checking the return value of a function call that signals failure via its return value. Depending on which function is being called, this defect can lead to seemingly random program misbehavior, including crashes and data corruptions in error conditions or low-resource situations.
In general, it is not safe to assume that a call to function requiring disk, network, memory, or other resources will always succeed. The caller should always check the return value and handle error cases appropriately.
The following code generates this warning:
#include <stdio.h> void f( ) { fopen( "test.c", "r" ); // return value ignored // code ... }
#include <stdio.h>void f( ){ fopen( "test.c", "r" ); // return value ignored // code ...}
To correct this warning, check the return value of the function as shown in the following code:
#include <stdio.h> void f( ) { FILE *stream; if((stream = fopen( "test.c", "r" )) == NULL ) return; // code ... }
#include <stdio.h>void f( ){ FILE *stream; if((stream = fopen( "test.c", "r" )) == NULL ) return; // code ...}
The following code uses safe function fopen_s to correct this warning:
#include <stdio.h> void f( ) { FILE *stream; errno_t err; if( (err = fopen_s( &stream, "test.c", "r" )) !=0 ) { // code ... } }
#include <stdio.h>void f( ){ FILE *stream; errno_t err; if( (err = fopen_s( &stream, "test.c", "r" )) !=0 ) { // code ... }}
This warning is also generated if the caller ignores the return value of a function annotated with theMustCheck property as shown in the following code:
#include <codeanalysis\sourceannotations.h> [returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );
#include <codeanalysis\sourceannotations.h>[returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );
void test_f()
{
func( ); //return value ignored
}
To correct the previous warning, check the return value as shown in the following code:
void test_f()
{
if( func( ) )
{
//code...
}
}
继续查MustCheck:
The MustCheck property specifies whether the caller must inspect the return value of a function. This property is used as a post condition on the return value of a function.
The MustCheck property must be set by using one of the following values:
SA_Yes - the return value must be checked; otherwise warning 6031 is issued.
SA_No - the return value should not be checked.
只有当MustCheck是SA_Yes时才会要求检查返回值(Checking a return value of a function marked with SA_No does not generate a warning.)
而windows SDK的__checkReturn就相当于(MustCheck=SA_Yes)
总结:检查返回值
- warning C6031: Return value ignored: 'GetLastError'
- Qt - 警告"warning: type qualifiers ignored on function return type"
- 警告"warning: type qualifiers ignored on function return type"
- Warning 534: Ignoring return value of function 'printf
- VS2010报错:warning C4715:not all control paths return a value
- What does “warning: not all control paths return a value” mean? (C++)
- WARNING: unknown config-key: url.redirect (ignored)
- linux kernel Kconfig warning: leading whitespace ignored
- Kernel Kconfig:warning: leading whitespace ignored
- The value (166) of MAXTRANS parameter ignored.
- RVO:Return value optimization
- expect match return value
- named return value optimization
- verify return value
- NRV (named return value)
- Named Return Value Optimization
- 62-形参return value
- System call return value
- 介绍memcache的安装
- 适配器模式
- 5.2滤波电路
- VC项目工程配置
- 广义特征值
- warning C6031: Return value ignored: 'GetLastError'
- c笔记
- SetUnhandledExceptionFilter + StackWalker
- 家庭云及其它(下)
- HTTP断点续传
- DCF握手机制
- 转VC各种问题
- VS2010编译缺少dll
- 教你如何迅速秒杀掉:99%的海量数据处理面试题 .