Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - how to find source of heap corruption errors

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 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)

like image 253
Danne Avatar asked Mar 18 '10 13:03

Danne


People also ask

How do you find the source of heap corruption?

Check for heap corruptionTry using the Global Flags Utility (gflags.exe) or pageheap.exe. See /windows-hardware/drivers/debugger/gflags-and-pageheap.

How do I debug a heap corruption?

If the calling subprogram then uses its own COM pointer, the system will generate an access violation. To debug heap corruption, you must identify both the code that allocated the memory involved and the code that deleted, released, or overwrote it.

How do you detect stack corruption?

When a stack corruption is detected, one should look at the local variables in the called and calling functions to look for possible sources of memory corruption. Check array and pointer declarations for sources of errors. Sometimes stray corruption of a processors registers might also be due to a stack corruption.

How do I know if heap is corrupted?

Then you can sprinkle calls to CheckForHeapCorruption() throughout your code, so that when heap corruption occurs it will be detected at the next call to CheckForHeapCorruption() rather than some time later on.


1 Answers

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.

like image 137
Andreas Brinck Avatar answered Oct 12 '22 11:10

Andreas Brinck