C6331
警告 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...
}
若要更正這個警告,請不要將 MEM_DECOMMIT 值傳遞給 VirtualFree 呼叫,如下列程式碼所示:
#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...
}