views:

132

answers:

2

I'm trying to debug a problem in a DLL written in C that keeps causing access violations. I'm using Visual C++ 2008, but the code is straight C.

I'm used to Delphi, where if an exception occurs while running under the debugger, the program will immediately break to the debugger and it will give you a chance to examine the program state. In Visual C++, though, all I get is a message in the Output tab:

First-chance exception at blah blah blah: Access violation reading location 0x04410000. No breaks, nothing. It just goes and unwinds the stack until it's back in my Delphi EXE, which recognizes something's wrong and alerts me there, but by that point I've lost several layers of call stack and I don't know what's going on.

I've tried other debugging techniques, but whatever it's doing is taking place deep within a nested loop inside a C macro that's getting called more than 500 times, and that's just a bit beyond my skill (or my patience) to trace through.

I figure there has to be some way to get the "first-chance" exception to actually give me a "chance" to handle it. There's probably some "break immediately on first-chance exceptions" configuration setting I don't know about, but it doesn't seem to be all that discoverable.

Does anyone know where it is and how to enable it?

+2  A: 

From the Debug menu, select Exceptions and check the boxes of the exceptions you'd like the debugger to break on. "Access Violation" is under "Win32 Exceptions."

James McNellis
Thanks. That worked. I wonder why Access Violation isn't checked by default...
Mason Wheeler
@Mason: No problem. I don't think any of them are checked by default. My guess is that there is a fairly significant performance penalty when "break on exception" is enabled (though I don't really know).
James McNellis
I wouldn't think so. I don't know C++ all that well, but in Delphi, the performance penalty is zero until it actually breaks on one, and when (if) it does, that usually means something has gone wrong and the debugger is exactly where you want to be.
Mason Wheeler
@Mason: I agree, I've always thought this was poor default behaviour.
BlueRaja - Danny Pflughoeft
A: 

You can also create a data breakpoint using the address specified in the "First-chance exception at..." line.

Following on from James' answer, the exceptions you're looking for are underneath the Win32 exceptions section. You should see Access Violation in there.

Mark Ingram
Would that work? This is a "read of" error, and AFAIK data breakpoints only go off if you change the value at the address in question.
Mason Wheeler
You can break when execution reaches a specific location too (I think it's just an option on the breakpoint properties dialog).
Mark Ingram