PageHeap1 is a Microsoft Visual C++ project with several types of memory errors. To enable PageHeap on this sample application, type in the following from the command line:
char * p;
p = new char;
If the end of the allocation is overwritten, the guard bytes change and PageHeap causes an Access Violation (AV) when the memory is freed. If the application reads or writes past the allocation (including the guard bytes), it incurs an instant AV.
How to use PageHeap1 sample
- Build the pgh project and run the pgh.exe.
Note You must do a release build for PageHeap to work with new/malloc.
Launch the PageHeap1.exe, and note that there is a dialog box pop-up.
- On the dialog box, you can see a TextBox, a "Bad Alloc/Free" check box, and three pair of buttons, "new" & "delete", "PageAlloc" & "Heap Free" and "COM new" & "COM Delete". The TextBox takes in the size of memory you want to have allocated. If the "Bad Alloc/Free" check box is selected, each allocation type (new, PageAlloc and COM new) allocates memory and then writes past the allocation. If "bad Alloc" is not checked, no memory overwrite occurs.
Button "new" tests the "new" operator, button "PageAlloc" tests HeapAlloc. The COM new does not use CoTaskMemAlloc but rather calls into a COM DLL that simply calls new. To test "COM new," you must either register r1LeakMemMod.dll or build the r1LeakMemMod project.
You must use a run-time DLL library for PageHeap to work. (From the Visual C++ IDE, Projects\settings\ C++ tab, Category: Code Generation, Use run-time library).
- After checking the box of "Bad Alloc/Free," if the memory allocation size is 5 bytes, click on the "new" button, 5 bytes memory will be allocated and "0" is written into the sixth byte. Writing to the sixth byte is an illegal memory overwrite, however it occurs on a guard byte so PageHeap does not detect this error until the memory is deleted. When you click the "delete" button, PageHeap detects the overwrite and you will see an error message box similar to following: The exception Breakpoint A breakpoint has been reached. (0x80000003) occurred in the application at location 0x77f9f9df.If you have Visual C++ specified as JIT debugger, you can click on the "Cancel" button and debug into the code.
If you change the allocation size to 8 (or any multiple of 8), selecting new, pageAlloc or "Com New" results in an instant AV because you have written to a address with no access. (i.e., you don't have to delete the memory to detect the error).
- Limitations: PageHeap can only find memory errors from the malloc family (hence C++ operator new) and heapAlloc. Many applications use custom allocators and PageHeap is unable to intercept these allocations.
- When you have finished testing an application run
pageheap /disable <appName>
from the command line to turn off PageHeap for that application.
- PageHeap enabled applications can consume much more memory than the same application without PageHeap enabled. You may have to increase your swap file to satisfy the increased memory demand.
Visual C++ .NETRelease Date: June 25, 2002
For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:
Article ID: 264471 - Last Review: Sep 20, 2012 - Revision: 1