Question

I've found information about this feature on SO some time ago, but the topic was a duplicate of Hidden Features of Visual Studio (2005-2008)? and I can't find it anymore.

I want to use something like this:

#ifdef DEBUG
#define break_here(condition) if (condition) ... // don't remember, what must be here
#else
#define break_here(condition) if (condition) return H_FAIL;
#endif
//...
hresult = do_something(...);
break_here(hresult != H_OK);
//...
var = do_other_thing(...);
break_here(var > MAX_VAR);

It must behave like a breakpoint on error. It's something like assertion, but no dialogs, and more light-weight.

I can't use normal breakpoints here, because my module is a part of several projects and can be edited in several VS solutions. This causes breakpoints, which was set in one solution, shift somewhere in source, when code edited in other solution.

Was it helpful?

Solution

Take a look at DebugBreak

Causes a breakpoint exception to occur in the current process. This allows the calling thread to signal the debugger to handle the exception.

example:

 var = do_other_thing(...);
 if (var > MAX_VAR)
      DebugBreak();

OTHER TIPS

I forget, I need ARM builds too and one of them is compiled not in MS Visual Studio :)

Moreover, it's better for me not to link additional code in library-version of my module. The need to include "winbase.h" for DebugBreak() was one "bad thing" of it, the better is to have some intrinsic. But this is little "bad thing", because there will be no breakpoints in final release :)

With help of crashmstr's answer I've found the alternatives of DebugBreak(). And now I'm using the following construction:

#ifdef _DEBUG

    #ifdef _MSC_VER
      #ifdef _X86_
        #define myDebugBreak { __asm { int 3 } }
      #else
        #define myDebugBreak  { __debugbreak(); } // need <intrin.h>
      #endif
    #else
      #define myDebugBreak { asm { trap } } // GCC/XCode ARM11 variant
    #endif

#else

      #define myDebugBreak

#endif

#define break_here(condition) if (condition) { myDebugBreak; return H_FAIL; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top