警告 C6332:无效的参数:不允许将 0 作为 dwFreeType 参数传递给 <function>。这会导致此调用失败
此警告意味着向 VirtualFree 或 VirtualFreeEx 传递的参数无效。VirtualFree 和 VirtualFreeEx 均拒绝为零的 dwFreeType 参数。dwFreeType 参数可以是 MEM_DECOMMIT 或 MEM_RELEASE。但是,MEM_DECOMMIT 和 MEM_RELEASE 值不能在同一个调用中使用。此外,还要确保 VirtualFree 函数的返回值不被忽略。
示例
在下面的代码中,因为向 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, 0 );
// code ...
}
若要更正此警告,请修改对 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 ...
}
就内存泄露以及内存异常而言,VirtualAlloc 与 VirtualFree 存在许多陷阱.若要完全避免这些泄漏和异常问题,请使用 C++ 标准模板库 (STL) 提供的结构。这些包括shared_ptr, unique_ptr, 和 vector有关更多信息,请参见智能指针(现代 C++)和C++ 标准库参考。