Search code examples
c++debuggingexceptioncomvisual-studio-debugging

How to use VS debugger to inspect a C++ exception object during the "first-chance"?


My program calls into a library for which I don't have source code (Direct2D ID2D1DCRenderTarget::BeginDraw). Visual Studio debugger gives me a "first-chance" when that library code throws an exception. I have the definition of the thrown object (it's a _com_error). How can I inspect that object in the debugger at that point?

I tried modifying my code to catch the exception, but the library is catching (and handling?) the exception before it propagates back to the call site. I tried poking around at the registers and memory in the debugger at the point of the first-chance exception, but I don't know enough about how VS maps exceptions into the ABI to really know where to look. Is there a particular register that points to the exception object?

Why I'm asking: I'm trying to figure out if this exception is part of the normal operation of the library or if it's indicative of a bug in how I'm using the library. The library appears to be handling whatever exceptional condition arises, but I'm not sure if it's just covering up a mistake on my part. In certain circumstances[*], it happens in every iteration of a hot loop[**], so I'm concerned about the performance impact of the exception propagation. I'm hoping the details in the _com_error exception object will give me a clue as to what's going on under the covers.

[*] The certain circumstances are that a high-contrast theme is selected. When a "standard" theme is selected, no exception is thrown.

[**] It's a hot loop because it's every frame of an animation. And, actually, it's a few times per frame because I'm animating on a few render targets simultaneously and the exception happens on the BeginDraw call for every target.


Solution

  • The address listed in the throw should be the address of a _com_error object. If he turns on the Microsoft Symbol Server, you should be able to inspect the properties of the _com_error and the IErrorInfo it’s holding internally.

    Example:

    Exception thrown at 0x757708F2 in ConsoleApplication1.exe: Microsoft C++ exception: _com_error at memory location 0x00BAFA70.

    Watch window:

    enter image description here

    However, I would suggest you turn on the sdk layers instead:

    https://msdn.microsoft.com/en-us/library/windows/desktop/ee794277(v=vs.85).aspx

    That should tell you if anything is going wrong with his D2D calls better than inspecting an error that may be intentional.