Edit

Share via


Warning C6332

Invalid parameter: passing zero as the dwFreeType parameter to 'function' is not allowed. This results in the failure of this call

This warning indicates that an invalid parameter is being passed to VirtualFree or VirtualFreeEx.

Remarks

VirtualFree and VirtualFreeEx both reject a dwFreeType parameter of zero. The dwFreeType parameter can be either MEM_DECOMMIT or MEM_RELEASE. However, the values MEM_DECOMMIT and MEM_RELEASE may not be used together in the same call. Also, make sure that the return value of the VirtualFree function isn't ignored.

Code analysis name: VirtualFreeInvalidParam2

Example

The following code generates warning C6332 because an invalid parameter is passed to the VirtualFree function:

#include <windows.h>
#define PAGELIMIT 80

DWORD dwPages = 0;  // count of pages
DWORD dwPageSize;   // page size

VOID f( VOID )
{
  LPVOID lpvBase;            // base address of the test memory
  BOOL bSuccess;
  SYSTEM_INFO sSysInfo;      // system information

  GetSystemInfo( &sSysInfo );
  dwPageSize = sSysInfo.dwPageSize;

  // Reserve pages in the process's virtual address space
  lpvBase = VirtualAlloc(
                         NULL,                // system selects address
                         PAGELIMIT*dwPageSize, // size of allocation
                         MEM_RESERVE,
                         PAGE_NOACCESS );
  if (lpvBase)
  {
    // code to access memory
  }
  else
  {
    return;
  }

  bSuccess = VirtualFree( lpvBase, 0, 0 );
  // code ...
}

To correct this warning, modify the call to the VirtualFree function, as shown in the following code:

#include <windows.h>
#define PAGELIMIT 80

DWORD dwPages = 0;  // count of pages
DWORD dwPageSize;   // page size

VOID f( VOID )
{
  LPVOID lpvBase;            // base address of the test memory
  BOOL bSuccess;
  SYSTEM_INFO sSysInfo;      // system information

  GetSystemInfo( &sSysInfo );
  dwPageSize = sSysInfo.dwPageSize;

  // Reserve pages in the process's virtual address space
  lpvBase = VirtualAlloc(
                         NULL,                // system selects address
                         PAGELIMIT*dwPageSize, // size of allocation
                         MEM_RESERVE,
                         PAGE_NOACCESS );
  if (lpvBase)
  {
    // code to access memory
  }
  else
  {
    return;
  }

  bSuccess = VirtualFree( lpvBase, 0, MEM_RELEASE );
  // code ...
}

The use of VirtualAlloc and VirtualFree has many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of potential leaks altogether, use the mechanisms that are provided by the C++ Standard Library (STL). These include shared_ptr, unique_ptr, and containers such as vector. For more information, see Smart pointers and C++ Standard Library.

See also

VirtualAlloc Method
VirtualFree Method