C6250

更新:2007 年 11 月

警告 C6250:不使用 MEM_RELEASE 标志调用 <function> 可能会释放内存,但不会释放地址说明符 (VAD);这样会导致地址空间泄漏

此警告意味着,如果在不使用 MEM_RELEASE 标志的情况下调用 VirtualFree,将仅仅解除页,而不会释放页。若要解除并释放页,应在对 VirtualFree 的调用中使用 MEM_RELEASE 标志。如果区域中的所有页都已提交,则函数将首先解除这些页,然后再释放它们。在执行该操作之后,这些页将处于可用状态。如果指定此标志,则在该区域已保留时,dwSize 必须为零,lpAddress 必须指向 VirtualAlloc 函数返回的基址。如果上述任一条件未得到满足,则函数将失败。

如果代码以后通过使用 MEM_RELEASE 标志调用 VirtualFree 来释放地址空间,则可以忽略此警告。

示例

下面的代码示例生成此警告:

#include <windows.h>
#include <stdio.h>
#define PAGELIMIT 80            

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

VOID f( )
{
  LPVOID lpvBase;            // base address of the test memory
  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);     
  //
  // code to access memory 
  // ...
 
  if (lpvBase != NULL)
  {
    if (VirtualFree( lpvBase, 0, MEM_DECOMMIT )) // decommit pages
      {
        puts ("MEM_DECOMMIT Succeeded");
      }
      else
      {
       puts("MEM_DECOMMIT failed");
      }
  }
  else
  {
    puts("lpvBase == NULL");
  }
}

若要更正此警告,请使用下面的代码示例:

#include <windows.h>
#include <stdio.h>
#define PAGELIMIT 80            

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

VOID f( )
{
  LPVOID lpvBase;            // base address of the test memory
  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);     
  //
  // code to access memory 
  // ...
 
  if (lpvBase != NULL)
  {
   if (VirtualFree(lpvBase, 0,MEM_RELEASE )) // decommit & release 
   {
     // code ...
   }
   else
   {
     puts("MEM_RELEASE failed");
   }
  }
  else 
  {
    puts("lpvBase == Null ");
    // code...
  }
}