C6331

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

warning C6331: Invalid parameter: passing MEM_RELEASE and MEM_DECOMMIT in conjunction to <function> is not allowed. This results in the failure of this call

This message indicates that an invalid parameter being passed to VirtualFree or VirtualFreeEx. VirtualFree and VirtualFreeEx both reject the flags (MEM_RELEASE | MEM_DECOMMIT) in combination. Therefore, the values MEM_DECOMMIT and MEM_RELEASE may not be used together in the same call.

It is not required for decommit and release to occur as independent steps. Releasing committed memory will decommit the pages as well. Also, ensure the return value of this function is not ignored.

Example

The following sample code generates this warning:

#include <windows.h>  
#define PAGELIMIT 80  
  
DWORD dwPages = 0;  // count of pages   
DWORD dwPageSize;   // page size   
  
VOID fd( 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_DECOMMIT | MEM_RELEASE); // warning   
  // code...  
}  

To correct this warning, do not pass MEM_DECOMMIT value to VirtualFree call 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...  
}  

Note that the use of malloc and free (and related dynamic memory allocation APIs) have many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.

See Also

VirtualAlloc Method
VirtualFree Method