Heap corruption

Heap corruption occurs when a program damages the allocator's view of the heap. The outcome can be relatively benign and cause a memory leak (where some memory isn't returned to the heap and is inaccessible to the program afterward), or it may be fatal and cause a memory fault, usually within the allocator itself. A memory fault typically occurs within the allocator when it manipulates one or more of its free lists after the heap has been corrupted.

It's especially difficult to identify the source of corruption when the source of the fault is located in another part of the code base. This is likely to happen if the fault occurs when:

  • a program attempts to free memory
  • a program attempts to allocate memory after it's been freed
  • the heap is corrupted long before the release of a block of memory
  • the fault occurs on a subsequent block of memory
  • contiguous memory blocks are used
  • your program is multithreaded
  • the memory allocation strategy changes