Use a memory allocator that performs runtime checking of heap integrity.
Identify corruption to the memory allocator data structures.
Which block of memory is being corrupted?
When is the corruption occurring?
If heap integrity is checked every time memory is allocated or freed, the application may become much slower.
If you only check the heap when memory is allocated and freed, many pointer references may occur in between. This tactic isn’t as strong as checking every pointer reference.
Make every pointer reference through one of two macros. One of them just resolves to the pointer reference. The other makes a function call, which calls the heap integrity checker before dereferencing the pointer. Change the first macro to the second, file by file, until you find the problem. This will cause a drastic slowdown in the program.
Implement wrapper functions around the standard memory allocation and deallocation procedures. The functions should maintain and check their own list of allocated storage.
Use a high-level debugger or interpreter to do the following:
Set a breakpoint at a place where you would like to begin checking the integrity of the heap.
Add a command list to the breakpoint that calls a user procedure to perform checking of heap integrity, and then continue execution.
Use this tactic when one of the following conditions is true:
A bug occurs when the program is run standalone, but not when it’s run under a debugger.
A bug occurs when the program is run under one user account (and environment), but not when the program is run under another.
Use this tactic at least once before the program is put into production, as an additional testing strategy.
C++: You can overload new and delete. You can also create special versions of new or delete, using the placement argument, that perform heap checking before using the normal form of these procedures to allocate storage.
Java: Java guarantees heap integrity, so the tactic is unnecessary.
C: Several publicly available versions of malloc and free perform heap integrity checking.
Fortran: Heap integrity can only be compromised by invalid array subscripts.