views:

337

answers:

4

Hi,

I wonder if there is a good way to find the source code that causes a heap corruption error, given the memory address of the of the data that was written 'outside' the allocated heap block in Visual Studio;

Dedicated (0008) free list element 26F7F670 is wrong size (dead)

(Trying to write down some notes on how to find memory errors)

Thanks in advance!

A: 

You could set a breakpoint on a write to the memory address. The debugger will then show you the code that writes to the location, but you still need to work out which of the writes are causing the problem.

Timo Geusch
A: 

I am assuming C++ as the language.

If the error is reproducible and the corrupted address is always the same, you can put a data breakpoint to stop the program when writing at this address.

Timores
Language is C/C++ mixed. The corrupted address is different at each debug session so I guess it's not possible to use a data breakpoint
Danne
You're unfortunately right.In these cases, my approach is to #define free/delete to be nothing. If the problem disappears, I #define malloc/new/free/delete to a function that logs each call, in order to find duplicate deletes or deletes without an allocation.
Timores
A: 

Maybe you can try Microsoft's Application Verifier. It solved a similar problem for me once,by turning on extra checks on heap operations. In my opinion, the randomness of corrupted address is because the heap can be 'subtly' damaged, and the problem won't show up until something big happens to the heap (like massive allocation/free).

Smithy
+1  A: 

Begin with installing windbg:

http://www.microsoft.com/whdc/Devtools/Debugging/default.mspx

Then turn on the pageheap like this:

gflags.exe –p /enable yourexecutable.exe /full

This will insert a non writable page after each heap allocation.

After this launch the executable from inside windbg, any writes outside the heap will now be caught by this debugger. To turn of the pageheap afterwards use this:

gflags.exe -p /disable yourexecutable.exe

More info on how to use the pageheap here.

Andreas Brinck