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.
Double delete of memory |
OnTestDoubledeleteofmemory() Allocate a character array using new(), and deletes the array twice |
Double free() of memory |
OnTestDoublefreeofmemory() Allocates a character array using malloc(), and frees the array twice |
Incorrect free() of memory |
OnTestIncorrectfreeofmemory() Allocates an array with malloc(), then frees pointers that point into the array, not at the start of it. |
Incorrect delete of memory |
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. |
Incorrect realloc() of memory |
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. |
Delete malloc'd memory |
OnMemoryerrorsIncorrectusageDeletemallocdmemory() Allocates with malloc(), deallocates with delete, then delete [], then free(). |
Delete realloc'd memory |
OnMemoryerrorsIncorrectusageDeletereallocdmemory() Allocates with malloc(), reallocates using realloc(), deallocates with delete, then delete [], then free(). |
Free new'd memory |
OnMemoryerrorsIncorrectusageFreenewdmemory() Allocates using new, deallocates the memory using free(), then deallocates using delete []. |
realloc() new'd memory |
OnMemoryerrorsIncorrectusageReallocnewdmemory() Allocates using new, reallocates using realloc(), then deallocates using delete []. |
Incorrect HeapRealloc() |
OnMemoryerrorsIncorrectusageIncorrectheaprealloc() Allocates 4096 bytes on the heap and tries to reallocate wrong locations inside and outside the allocated area |
Incorrect HeapFree() |
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.
Allocate memory and overwrite end of memory |
OnTestAllocatememoryandoverwriteendofmemory() Prompts for a memory size to allocate, then zeros 1 byte after the end of memory. |
Allocate memory and overwrite beginning 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.
Objects on Stack |
OnUninitialiseddataObjectsonstack() Creates a TestClass object on the stack. |
Objects on Heap |
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.
Overrun allocated memory |
OnBufferoverrunOverun() Allocates 100 bytes using new, and uses memcpy() to copy memory, deliberately overruning the end. |
Underrun allocated memory |
OnBufferoverrunUnderrun() As above but underruning the start |
Overrun stack memory |
OnBufferoverrunOverrunstackmemory() Declares a 100 byte array on the stack, and uses memcpy() to copy, deliberately overruning the end of the array |
Underrun stack memory |
OnBufferoverrunUnderrunstackmemory() As above but underruning the start of the array |
strcpy and wcscpy test |
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.
Speculative Leak Test1 |
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. |
Speculative Leak Test2 |
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. |
Allocate 0 bytes |
OnLeaksAllocate0bytes() Allocates an array of 0 characters using new |
Allocate -10 bytes |
OnLeaksAllocate10bytes() Allocates an array of -10 characters using new |
Allocate and don't use memory before deleting memory |
OnLeaksAllocateanddontusememorybeforedeletingmemory() Allocates memory using new, then deletes it without using it. |
Allocate a class that allocates data and leaks |
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. |
Deeply recursive call to test hotspots |
OnMemoryerrorsSpecialDeeplyrecursivecalltotestcallstacks() Allocates and leaks an array of 50 bytes on the way out of each level of a 1500 level recursive call. |
Deleted this usage |
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.
Message Map Error |
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. |