Programming Tips - How can I fix a "Free Heap block number modified at number after it

Date: 2010mar24 OS: Windows Platform: Visual C++ Keywords: malloc, free Q. How can I fix a "Free Heap block <number> modified at <number> after it was freed" error? This is the full message:
HEAP[MyProgram.exe]: HEAP: Free Heap block 3682378 modified at 3682edc after it was freed Windows has triggered a breakpoint in MyProgram.exe. This may be due to a corruption of the heap, and indicates a bug in MyProgram.exe or any of the DLLs it has loaded. The output window may have more diagnostic information
A. The problem is that you get this break a little while after you have illegally modified a free object... not right when you do it. So the code you see in your stack trace is just the innocent bystander. There are a number of ways to solve this problem. But one that worked for me is to sprinkle this code:
through my program before the break point. This will cause a break if your memory is bad. So you can see that everything was OK after function something() but bad before function something_else(). And you can do a binary search to zero in on the problem spot.