Monday, June 01, 2009

Heap Debugging (Memory/Resource Leak) with WinDbg

I recently had to do some heap debugging to solve an issue at work and it was a bit of a pain in the butt because there are several steps that I needed to take to set everything up. Here's what I had to do...

  1. First, you need the Debugging Tools for Windows installed.
  2. Open a command prompt and navigate to the debugging tools folder (usually C:\Program Files\Debugging Tools for Windows (x86).
  3. Type "gflags.exe /i yourApplication.exe +ust" (without the quotes). This command creates a user mode stack trace database that will be used later.
  4. Launch the application in WinDbg.
  5. Recreate the error and then break into the debugger.
  6. Run the command "!heap -s". This will display a summary of all of the current heaps. Find the one that seems to be bloated.
  7. Run the command "!heap -stat -h " on the block that appears to be bloated. This will display statistics on the heap's allocations by allocation block size. My problem was that one resource type was being leaked so there was one block size that was used far more frequently than the others. Find that block size.
  8. Run the command "!heap -flt s ". This will filter the heap to only show heaps of that particular size.
  9. Now choose one of the UserPtr's in the list and run the command "!heap -p -a " on it. This will display information on that particular block. It should also include a stack trace from when that memory was allocated.
  10. Remember to remove the global flags by doing "gflags.exe /i -ust" when you're done.

No comments:

Post a Comment