更新:2007 年 11 月
警告 C6331:无效的参数: 不允许将 MEM_RELEASE 和 MEM_DECOMMIT 与 <function> 一起传递。这会导致此调用失败
此消息意味着向 VirtualFree 或 VirtualFreeEx 传递的参数无效。VirtualFree 和 VirtualFreeEx 均拒绝组合使用标志 (MEM_RELEASE|MEM_DECOMMIT)。因此,MEM_DECOMMIT 和 MEM_RELEASE 值不能在同一个调用中使用。
解除和释放无需作为独立的步骤发生。释放已提交的内存将同时解除页。还要确保此函数的返回值不被忽略。
示例
下面的代码示例生成此警告:
#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...
}
若要更正此警告,请不要向 VirtualFree 调用传递 MEM_DECOMMIT 值,如下面的代码所示:
#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...
}