Edit

Share via


Warning C6219

Implicit cast between semantically different integer types: comparing HRESULT to 1 or TRUE. Consider using SUCCEEDED or FAILED macro instead

Remarks

This warning indicates an HRESULT is being compared with an explicit, non-HRESULT value of one (1). This comparison is likely to lead to incorrect results, because the typical success value of HRESULT (S_OK) is 0. If you compare this value to a Boolean type, it's implicitly converted to false.

Code analysis name: COMPARING_HRESULT_TO_ONE

Example

The following code generates this warning because the CoGetMalloc returns an HRESULT, which then is compared to TRUE:

#include <windows.h>

void f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  hr = CoGetMalloc(1, &pMalloc);

  if (hr == TRUE)
  {
    // success code ...
  }
  else
  {
    // failure code
  }
}

Most of the time, this warning is caused by code that compares an HRESULT to a Boolean. It's better to use the SUCCEEDED or FAILED macros to test the value of an HRESULT. To correct this warning, use the following code:

#include <windows.h>

void f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  hr = CoGetMalloc(1, &pMalloc);

  if (SUCCEEDED(hr))
  {
    // success code ...
  }
  else
  {
    // failure 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.