Visual Studio - can be a breakpoint called from code?

breakpoints, c++, debugging, visual-c++, visual-studio

Solution

Use the `__debugbreak()` intrinsic(requires the inclusion of `<intrin.h>`).

Using `__debugbreak()` is preferable to directly writing `__asm { int 3 }` since inline assembly is not permitted when compiling code for the x64 architecture.

And for the record, on Linux and Mac, with GCC, I'm using `__builtin_trap()`.

Problem

I have a unit test project based on UnitTest++. I usually put a breakpoint to the last line of the code so that the I can inspect the console when one of the tests fails: ``` n = UnitTest::RunAllTests(); if ( n != 0 ) { // place breakpoint here return n; } return n; ``` But I have to reinsert it each time I check-out the code anew from SVN. Is it possible to somewhat place the breakpoint by the compiler?: ``` n = UnitTest::RunAllTests(); if ( n != 0 ) { // place breakpoint here #ifdef __MSVC__ @!!!$$$??___BREAKPOINT; #endif return n; } return n; ```

Original source