Example 2: Finding Dangling Pointer References
Hoarding is most often used to find dangling pointer references. Once you know the problem is related to a dangling pointer, you need to locate where your program deallocated the memory. One technique is to use block tagging (see
Task 6: “Using Runtime Events”). Another is to use block painting to write a pattern into deallocated memory. If you also hoard painted memory, the heap manager will not be able to reallocate the memory as quickly.
If the memory was not hoarded, the heap manager could reallocate the memory block. When it is reallocated, a program can legitimately use the block, changing the data in the painted memory. If this occurs, the block is both legitimately allocated and its contents are legitimate in some context. However, the older context was destroyed. Hoarding delays the recycling of the block. In this way, it extends the time available for you to detect that your program is accessing deallocated memory.