Skip to content

Commit 65c16bc

Browse files
Updated C6333
Updated to new format, modified wording, changed whitespace to match my other PRs
1 parent 45cfccf commit 65c16bc

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

docs/code-quality/c6333.md

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
---
22
description: "Learn more about: C6333"
33
title: C6333
4-
ms.date: 11/04/2016
4+
ms.date: 08/25/2022
55
ms.topic: reference
6-
f1_keywords: ["C6333"]
6+
f1_keywords: ["C6333", "VIRTUALFREEINVALIDPARAM3", "__WARNING_VIRTUALFREEINVALIDPARAM3"]
77
helpviewer_keywords: ["C6333"]
88
ms.assetid: 4b8fa4b2-a3a0-4d00-bec7-76686b66fcf9
99
---
10-
# C6333
10+
# Warning C6333
1111

12-
> warning C6333: Invalid parameter: passing MEM_RELEASE and a non-zero dwSize parameter to \<function> is not allowed. This results in the failure of this call
12+
> 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
1313
14-
This warning indicates an invalid parameter is being passed to VirtualFree or VirtualFreeEx. Both of these functions reject a dwFreeType of MEM_RELEASE with a non-zero value of dwSize. When passing MEM_RELEASE, the dwSize parameter must be zero. Also, make sure that the return value of this function is not ignored.
14+
## Remarks
15+
16+
Both `VirtualFree` and `VirtualFreeEx` reject a `dwFreeType` of `MEM_RELEASE` with a non-zero value of `dwSize`. When passing `MEM_RELEASE`, the `dwSize` parameter must be zero.
17+
18+
Code analysis name: VIRTUALFREEINVALIDPARAM3
1519

1620
## Example
1721

18-
The following sample code generates this warning:
22+
The following code sample generates this warning:
1923

2024
```cpp
2125
#include <windows.h>
@@ -24,36 +28,36 @@ The following sample code generates this warning:
2428
DWORD dwPages = 0; // count of pages
2529
DWORD dwPageSize; // page size
2630

27-
VOID f( VOID )
31+
VOID f(VOID)
2832
{
29-
LPVOID lpvBase; // base address of the test memory
30-
BOOL bSuccess;
31-
SYSTEM_INFO sSysInfo; // system information
32-
33-
GetSystemInfo( &sSysInfo );
34-
dwPageSize = sSysInfo.dwPageSize;
35-
36-
// Reserve pages in the process's virtual address space
37-
lpvBase = VirtualAlloc(
38-
NULL, // system selects address
39-
PAGELIMIT*dwPageSize,// size of allocation
40-
MEM_RESERVE,
41-
PAGE_NOACCESS );
42-
if (lpvBase)
43-
{
44-
// code to access memory
45-
}
46-
else
47-
{
48-
return;
49-
}
50-
51-
bSuccess = VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_RELEASE);
52-
//code...
33+
LPVOID lpvBase; // base address of the test memory
34+
BOOL bSuccess;
35+
SYSTEM_INFO sSysInfo; // system information
36+
37+
GetSystemInfo(&sSysInfo);
38+
dwPageSize = sSysInfo.dwPageSize;
39+
40+
// Reserve pages in the process's virtual address space
41+
lpvBase = VirtualAlloc(
42+
NULL, // system selects address
43+
PAGELIMIT*dwPageSize,// size of allocation
44+
MEM_RESERVE,
45+
PAGE_NOACCESS);
46+
if (lpvBase)
47+
{
48+
// code to access memory
49+
}
50+
else
51+
{
52+
return;
53+
}
54+
55+
bSuccess = VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_RELEASE);
56+
//code...
5357
}
5458
```
5559
56-
To correct this warning, use the following sample code:
60+
To correct this warning, ensure that the value of `dwSize` is 0 in the call to `VirtualFree`:
5761
5862
```cpp
5963
#include <windows.h>
@@ -62,37 +66,37 @@ To correct this warning, use the following sample code:
6266
DWORD dwPages = 0; // count of pages
6367
DWORD dwPageSize; // page size
6468
65-
VOID f( VOID )
69+
VOID f(VOID)
6670
{
67-
LPVOID lpvBase; // base address of the test memory
68-
BOOL bSuccess;
69-
SYSTEM_INFO sSysInfo; // system information
70-
71-
GetSystemInfo( &sSysInfo );
72-
dwPageSize = sSysInfo.dwPageSize;
73-
74-
// Reserve pages in the process's virtual address space
75-
lpvBase = VirtualAlloc(
76-
NULL, // system selects address
77-
PAGELIMIT*dwPageSize,// size of allocation
78-
MEM_RESERVE,
79-
PAGE_NOACCESS );
80-
if (lpvBase)
81-
{
82-
// code to access memory
83-
}
84-
else
85-
{
86-
return;
87-
}
88-
bSuccess = VirtualFree(lpvBase, 0, MEM_RELEASE );
89-
90-
// VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_DECOMMIT);
91-
// code...
71+
LPVOID lpvBase; // base address of the test memory
72+
BOOL bSuccess;
73+
SYSTEM_INFO sSysInfo; // system information
74+
75+
GetSystemInfo(&sSysInfo);
76+
dwPageSize = sSysInfo.dwPageSize;
77+
78+
// Reserve pages in the process's virtual address space
79+
lpvBase = VirtualAlloc(
80+
NULL, // system selects address
81+
PAGELIMIT*dwPageSize, // size of allocation
82+
MEM_RESERVE,
83+
PAGE_NOACCESS);
84+
if (lpvBase)
85+
{
86+
// code to access memory
87+
}
88+
else
89+
{
90+
return;
91+
}
92+
bSuccess = VirtualFree(lpvBase, 0, MEM_RELEASE);
93+
94+
// VirtualFree(lpvBase, PAGELIMIT * dwPageSize, MEM_DECOMMIT);
95+
// code...
9296
}
9397
```
9498

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

97101
## See also
98102

0 commit comments

Comments
 (0)