Edit

Share via


Warning C6333

Invalid parameter: passing MEM_RELEASE and a non-zero dwSize parameter to 'function_name' is not allowed. This results in the failure of this call

Remarks

Both VirtualFree and VirtualFreeEx reject a dwFreeType of MEM_RELEASE with a non-zero value of dwSize. When MEM_RELEASE is passed, the dwSize parameter must be zero.

Code analysis name: VIRTUALFREEINVALIDPARAM3

Example

The following code sample generates this warning:

#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, PAGELIMIT * dwPageSize, MEM_RELEASE);
    //code...
}

To correct this warning, ensure that the value of dwSize is 0 in the call to 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);

    //  VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_DECOMMIT);
    // code...
}

You can also use VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_DECOMMIT); call to decommit pages, and later release them using MEM_RELEASE flag.

See also