This section tests and demonstrates different types of memory error, including:
•mismatched reallocations and deallocations
•memory and stack corruption
•uninitialised data
•buffer underrun and overrun
•other incorrect usage that can be detected
Each test is listed below with the source code location in the example application.
Click on the picture below to jump to the relevant sections:
Memory Errors menu
Incorrect Usage submenu
...
Demonstrates detection of incorrect methods for reallocating or deallocating allocated memory using C Runtime Heap functions.
Includes memory that has not been allocated, or pointers near to pointers that have been allocated.
|
OnTestDoubledeleteofmemory() Allocate a character array using new(), and deletes the array twice |
|
OnTestDoublefreeofmemory() Allocates a character array using malloc(), and frees the array twice |
|
OnTestIncorrectfreeofmemory() Allocates an array with malloc(), then frees pointers that point into the array, not at the start of it. |
|
OnTestIncorrectdeleteofmemory() Delete a pointer and an array pointer that is not in the heap. Allocates an array, and tries to delete pointers that point into the array, but not at the start. Deletes the array using the incorrect object form of delete. Allocates an array, and deletes it using the correct array form of delete. Allocates an array, and deletes it incorrectly with object form of delete. Allocates an object and deletes it using the wrong array form of delete. |
|
OnTestIncorrectreallocofmemory() Tries to reallocate a pointer that is not in the heap. Allocates using new, then tries to reallocate using realloc(), before deleting. Allocates using malloc(), then reallocates using pointers before and after the correct memory location. |
|
OnMemoryerrorsIncorrectusageDeletemallocdmemory() Allocates with malloc(), deallocates with delete, then delete [], then free(). |
|
OnMemoryerrorsIncorrectusageDeletereallocdmemory() Allocates with malloc(), reallocates using realloc(), deallocates with delete, then delete [], then free(). |
|
OnMemoryerrorsIncorrectusageFreenewdmemory() Allocates using new, deallocates the memory using free(), then deallocates using delete []. |
|
OnMemoryerrorsIncorrectusageReallocnewdmemory() Allocates using new, reallocates using realloc(), then deallocates using delete []. |
|
OnMemoryerrorsIncorrectusageIncorrectheaprealloc() Allocates 4096 bytes on the heap and tries to reallocate wrong locations inside and outside the allocated area |
|
OnMemoryerrorsIncorrectusageIncorrectheapfree() Allocates 4096 bytes and tries to free wrong locations inside and outside the allocated area |
Memory Errors menu
Memory Corruption submenu
...
Demonstrates corruption by overwriting the start and end of arrays allocated using C Runtime Heap functions.
|
OnTestAllocatememoryandoverwriteendofmemory() Prompts for a memory size to allocate, then zeros 1 byte after the end of memory. |
|
OnTestAllocatememoryandoverwritebeginningofmemory() Prompts for a memory size to allocate, then zeros 1 byte before the start. |
Memory Errors menu
Uninitialised Data submenu
...
Demonstrates detection of uninitialized data in C++ objects on both the stack and the C runtime heap.
|
OnUninitialiseddataObjectsonstack() Creates a TestClass object on the stack. |
|
OnUninitialiseddataObjectsonheap() Creates a TestClass object on the heap, then deletes it. |
Memory Errors menu
Buffer Overrun submenu
...
Demonstrates buffer overrun and buffer underrun detection on the C Runtime heap and the stack.
Underruning the stack is usually benign, but overrunning it usually results in a program crash as the function call return address has usually been damaged.
|
OnBufferoverrunOverun() Allocates 100 bytes using new, and uses memcpy() to copy memory, deliberately overruning the end. |
|
OnBufferoverrunUnderrun() As above but underruning the start |
|
OnBufferoverrunOverrunstackmemory() Declares a 100 byte array on the stack, and uses memcpy() to copy, deliberately overruning the end of the array |
|
OnBufferoverrunUnderrunstackmemory() As above but underruning the start of the array |
|
OnMemoryerrorsBufferoverrunStrcpyandwcscpytests() Allocate some character arrays and use these functions to overwrite beginning end of the buffers |
Memory Errors menu
Special submenu
...
Demonstrates some of Memory Validator's heap watching functions.
|
OnLeaksSpeculativeleaktest1() Allocates an array of 10 pointers and a CString object for each one. Deletes the array, but not the pointers. If speculative leak detection is enabled, the leaked objects will be identified. |
|
OnLeaksSpeculativeleaktest2() Allocates an array of 4 pointers populated with a CWnd, CBrush, CPen and CWnd objects. DeleteS the array, but not the pointers. If speculative leak detection is enabled, the leaked objects will be identified. |
|
OnLeaksAllocate0bytes() Allocates an array of 0 characters using new |
|
OnLeaksAllocate10bytes() Allocates an array of -10 characters using new |
|
OnLeaksAllocateanddontusememorybeforedeletingmemory() Allocates memory using new, then deletes it without using it. |
|
OnMemoryerrorsSpecialAllocateaclassthatallocatesdataandleaks() An allocateInsideThis object is allocated. Functions are called that allocate but don't free data held by the object, and then it's deleted. If speculative leak detection is enabled, leaked objects will be identified. |
|
OnMemoryerrorsSpecialDeeplyrecursivecalltotestcallstacks() Allocates and leaks an array of 50 bytes on the way out of each level of a 1500 level recursive call. |
|
OnMemoryerrorsDeletedthisusage() Allocates a class, deletes it and then calls a method in the deleted class. If detection of calling functions for deleted objects is enabled, this will be identified. |
This test is only enabled in Visual Studio versions prior to 7.0 as the code will not compile in that version or above.
|
OnMemoryerrorsMessagemaperror() Calls this function with too many parameters despite a signature with no params. If detection of Broken Message Map hooks is enabled, this will be identified in Debug and Release builds. |