Warning C6226
Implicit cast between semantically different integer types: assigning -1 to HRESULT. Consider using E_FAIL instead.
This warning indicates that an HRESULT
is assigned or initialized to an explicit value of -1.
Remarks
This warning is frequently caused by accidental confusion of integer and HRESULT
types. To indicate success, use the symbolic constant S_OK
instead. To indicate failure, use the symbolic constants that start with E_constant, such as E_FAIL
.
For more information, see the SUCCEEDED
and FAILED
macros.
Code analysis name: ASSIGNING_MINUS_ONE_TO_HRESULT
Example
The following code generates this warning:
#include <windows.h>
VOID f( )
{
HRESULT hr;
LPMALLOC pMalloc;
if (FAILED(CoGetMalloc(1, &pMalloc)))
{
hr = -1;
// code ...
}
else
{
// code ...
}
}
To correct this warning, use the following code:
#include <windows.h>
VOID f( )
{
HRESULT hr;
LPMALLOC pMalloc;
if (FAILED(CoGetMalloc(1, &pMalloc)))
{
hr = E_FAIL;
// code ...
}
else
{
// code ...
}
}
For this warning, the SCODE
type is treated as an HRESULT
.
The use of malloc
and free
(and related dynamic memory APIs) has many pitfalls as a cause of memory leaks and exceptions. To avoid these kinds of leaks and exception problems, use the pointer and container classes provided by the C++ Standard Library. These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.