Condividi tramite


Avviso C6332

Parametro non valido: il passaggio di zero come parametro dwFreeType a 'function' non è consentito. In questo modo si verifica l'errore di questa chiamata

Questo avviso indica che viene passato un parametro non valido a VirtualFree o VirtualFreeEx.

Osservazioni:

VirtualFree e VirtualFreeEx entrambi rifiutano un dwFreeType parametro pari a zero. Il dwFreeType parametro può essere MEM_DECOMMIT o MEM_RELEASE. Tuttavia, i valori MEM_DECOMMIT e MEM_RELEASE non possono essere usati insieme nella stessa chiamata. Assicurarsi inoltre che il valore restituito della VirtualFree funzione non venga ignorato.

Nome dell'analisi del codice: VirtualFreeInvalidParam2

Esempio

Il codice seguente genera l'avviso C6332 perché alla funzione viene passato VirtualFree un parametro non valido:

#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 ...
}

Per correggere questo avviso, modificare la chiamata alla VirtualFree funzione, come illustrato nel codice seguente:

#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 ...
}

L'uso di VirtualAlloc e VirtualFree presenta molte insidie in termini di perdite di memoria ed eccezioni. Per evitare completamente questi tipi di potenziali perdite, usare i meccanismi forniti dalla libreria standard C++ (STL). Sono inclusi shared_ptri contenitori , e , unique_ptrad esempio vector. Per altre informazioni, vedere Puntatori intelligenti e libreria standard C++.

Vedi anche

VirtualAlloc Metodo
VirtualFree Metodo