Debug Visual C++ memory allocation problems

2019-03-06 06:56发布

问题:

I'm debugging a software which crashes eventually with one of the following messages:

1. DAMAGE: after normal block (#24729280) at 0x00D710E0
2. Debug Assertion Failed
   Program: D:\Soft\Test.exe
   File: dbgheap.c
   Line: 1017

   Expression: _BLOCK_TYPE_IS_VALID(phead->nBlockUse)

This software is really old but changing it now is not an option. It's written on Visual C++ 6.0. We are guessing it's some kind of buffer overflow, so we are trying to find ways to detect where it is happening.

I have found information about PageHeap (which seems to be able to tell me what I want) and GFlags, but it seems I can't make it work.

I created a test program:

char* test;
test = new char[5];
test[5] = 'a';
delete[] test;

which raises an error:

DAMAGE: after normal block (#55) at 0x1671920

Then, I tried attaching PageHeap to it by running:

gflags.exe /p /enable MemoryTest.exe /full

and then rerunning it (both through Visual C++ 6.0 interface and through the windows explorer), which resulted on the same error.

Then I tried to compile the release version, and ran it through the Visual C++ 6.0 interface to get the error:

User breakpoint called from code at 0x7c90120e

And from the windows explorer, I just got the windows dialog asking me to send an error report.

What am I missing?

回答1:

You can run your application in release mode by attaching to Windbg.

  • Enable the gflags ( As you mentioned)
  • Start the application in release mode.
  • Attach it to Windbg using Attach to process option in Windbg.
  • Configure the correct path for release PDBs.
  • Reload the PDB manually using .reload /f in case of automatic loading fails.
  • Perform the use case.

WinDbg would stop the execution whenever an exception occurs. For every first chance exception, analyze the reasons. It could be one of the error for crash.



回答2:

Before using gFlags/PageHeap I suggest you to check for Access Violation exception. First attach the process by using Build->Start Debug->Attach to process option. Once it is attached enable the access violation exception by going to Debug->Exceptions select Access Violation and select the check box Stop Always. Then check whether your debugger catches any access violation exceptions.