7

It was a bug that I just found! Hooray. The bug was due to an incorrect downcasting, and indeed I was using static_cast instead of dynamic_cast.

My application is pretty large and multithreaded and interacts with other applications. So debugging is very hard. I have tried to use WinDbg, GFlags, and Application Verifier without results. Certainly because I don't know how to use these tools.

Is it possible to find a memory heap corruption due to an invalid downcasting, with the use of tools like WinDbg? If yes, how?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145

2 Answers2

13

Windbg !heap –s –v command can reveal a corrupt heap

0:008> !heap -s -v

  Heap     Flags   Reserv  Commit  Virt   Free  List   UCR  Virt  Lock  Fast 
                (k)     (k)    (k)     (k) length      blocks cont. heap 
-----------------------------------------------------------------------------
.ERROR: Block 001842e8 previous size 0 does not match previous block size 4
HEAP 00140000 (Seg 00140640) At 001842e8 Error: invalid block Previous
Kjell Gunnar
  • 3,017
  • 18
  • 24
1

EDIT: Comments made it clear that non-Windows options aren't viable. In that case I've had good luck with Purify before, but unfortunately it's $$$. I'm not familiar with other Windows memory checking tools however.

In regards to this specific case, anytime you find yourself downcasting, spend at least a minute thinking about an alternate interface or design that could remove the need. Compiler errors and warnings, and a solid design can find a lot of bugs that would otherwise take hours to find.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • recommending valgrind for a windows user? – PlasmaHH Jan 05 '12 at 16:17
  • @Mark B: so you confirm it's possible with valgrind to have in input the dump of app when it crash and on output the line of code which cause the problem ? – Guillaume Paris Jan 05 '12 at 16:19
  • @PlasmaHH: yes indeed but I think there is the equivalent tool under windows – Guillaume Paris Jan 05 '12 at 16:20
  • I didn't see any indication in the question that running on an alternate platform was out of the question for debugging purposes. @Guillaume07 I don't think there's any program in the world that can take a core dump (or window equivalent) and tell you which line corrupted the heap. You have to run the program under the analysis tool to get useful information. – Mark B Jan 05 '12 at 17:18