Please enable JavaScript to view this site.

Memory Validator Help

 

The Memory Errors menu

 

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.

 

seeAlsoHow to use these examples

 

Click on the picture below to jump to the relevant sections:

 

interactive

example-app-mem-errs-menu

 

 

Memory Errors menu : Incorrect Usage

 

menu Memory Errors menu instructionStep Incorrect Usage submenu instructionStep ...

 

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.

 

instructionStep Double delete of memory

OnTestDoubledeleteofmemory()

Allocate a character array using new(), and deletes the array twice

instructionStep Double free() of memory

OnTestDoublefreeofmemory()

Allocates a character array using malloc(), and frees the array twice

instructionStep Incorrect free() of memory

OnTestIncorrectfreeofmemory()

Allocates an array with malloc(), then frees pointers that point into the array, not at the start of it.

instructionStep 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.

instructionStep 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.
Reallocates the memory using realloc(), then frees the memory.

instructionStep Delete malloc'd memory

OnMemoryerrorsIncorrectusageDeletemallocdmemory()

Allocates with malloc(), deallocates with delete, then delete [], then free().
Allocates memory with calloc(), deallocate with delete, then delete [], then free().

instructionStep Delete realloc'd memory

OnMemoryerrorsIncorrectusageDeletereallocdmemory()

Allocates with malloc(), reallocates using realloc(), deallocates with delete, then delete [], then free().

instructionStep Free new'd memory

OnMemoryerrorsIncorrectusageFreenewdmemory()

Allocates using new, deallocates the memory using free(), then deallocates using delete [].

instructionStep realloc() new'd memory

OnMemoryerrorsIncorrectusageReallocnewdmemory()

Allocates using new, reallocates using realloc(), then deallocates using delete [].

instructionStep Incorrect HeapRealloc()

OnMemoryerrorsIncorrectusageIncorrectheaprealloc()

Allocates 4096 bytes on the heap and tries to reallocate wrong locations inside and outside the allocated area

instructionStep Incorrect HeapFree()

OnMemoryerrorsIncorrectusageIncorrectheapfree()

Allocates 4096 bytes and tries to free wrong locations inside and outside the allocated area

 

 

Memory Errors menu :  Memory Corruption

 

menu Memory Errors menu instructionStep Memory Corruption submenu instructionStep ...

 

Demonstrates corruption by overwriting the start and end of arrays allocated using C Runtime Heap functions.

 

instructionStep Allocate memory and overwrite end of memory

OnTestAllocatememoryandoverwriteendofmemory()

Prompts for a memory size to allocate, then zeros 1 byte after the end of memory.

instructionStep 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

 

menu Memory Errors menu instructionStep Uninitialised Data submenu instructionStep ...

 

Demonstrates detection of uninitialized data in C++ objects on both the stack and the C runtime heap.

 

instructionStep Objects on Stack

OnUninitialiseddataObjectsonstack()

Creates a TestClass object on the stack.
Some of its constructors deliberately leave data members uninitialized so can be tracked.

instructionStep Objects on Heap

OnUninitialiseddataObjectsonheap()

Creates a TestClass object on the heap, then deletes it.

 

 

Memory Errors menu :  Buffer Overrun

 

menu Memory Errors menu instructionStep Buffer Overrun submenu instructionStep ...

 

Demonstrates buffer overrun and buffer underrun detection on the C Runtime heap and the stack.
 

note 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.

 

 

instructionStep Overrun allocated memory

OnBufferoverrunOverun()

Allocates 100 bytes using new, and uses memcpy() to copy memory, deliberately overruning the end.

instructionStep Underrun allocated memory

OnBufferoverrunUnderrun()

As above but underruning the start

instructionStep Overrun stack memory

OnBufferoverrunOverrunstackmemory()

Declares a 100 byte array on the stack, and uses memcpy() to copy, deliberately overruning the end of the array

instructionStep Underrun stack memory

OnBufferoverrunUnderrunstackmemory()

As above but underruning the start of the array

instructionStep strcpy and wcscpy test

OnMemoryerrorsBufferoverrunStrcpyandwcscpytests()

Allocate some character arrays and use these functions to overwrite beginning  end of the buffers

 

 

Memory Errors menu : Special

 

menu Memory Errors menu instructionStep Special submenu instructionStep ...

 

Demonstrates some of Memory Validator's heap watching functions.

 

 

instructionStep 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.

instructionStep 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.

instructionStep Allocate 0 bytes

OnLeaksAllocate0bytes()

Allocates an array of 0 characters using new

instructionStep Allocate -10 bytes

OnLeaksAllocate10bytes()

Allocates an array of -10 characters using new

instructionStep Allocate and don't use memory before deleting memory

OnLeaksAllocateanddontusememorybeforedeletingmemory()

Allocates memory using new, then deletes it without using it.
If the detect unused memory feature is enabled, the unused memory will be reported.

instructionStep 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.

instructionStep 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.

 

 

Memory Errors menu :  Deleted this usage

 

menu 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.

 

 

Memory Errors menu :  Message Map Error

 

This test is only enabled in Visual Studio versions prior to 7.0 as the code will not compile in that version or above.

 

menu 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.