javaLover
javaLover

Reputation: 6425

stop Visual Studio from moving my breakpoint, when run "optimization=Enable"

I have added a breakpoint (the red oblique circle left of a code line) in one of my functions (#A-line).

public: B(const Vector3& move){
    this->move_=move;
    if(!Vector3::isValid(move)){
        int asdf=0;                //#A
    }
    basis_=Matrix3x3::identity();  //#B
}

If it run in debug mode, or release mode without optimization, the program will pause at #A correctly.

If it run in release mode with optimization, the breakpoint will move to #B by itself!
Thus, the program will pause every time it passes the function.

I know that when optimized, the line #A would be optimized out.
As a result, the line would never be passed.
I expect that it would never pause in the function at all.

The actual behavior is : It always pauses at #B.
It seems to be a bug of Visual Studio for me. (?)

Question

How to make the program not pause at #B?
In other words, how to force Visual Studio to not shift the breakpoint automatically?

Upvotes: 0

Views: 1015

Answers (3)

GeorgeT
GeorgeT

Reputation: 504

If your purpose is to debug a release build and a particular breakpoint is essential for the debugging, you could turn off all optimizations for the particular source file only (C++\Optimization properties of the cpp). Of course this would alter your program's behavior, and is not your best option.

Another possibility is to inject some trace commands before and after line with //#A so that they would not be eliminated by the optimizer and you could break on them:

if(!Vector3::isValid(move)){
//some tracing to force debugger to break here
        int asdf=0;                //#A
//some more tracing to force debugger to break here
    }

Upvotes: 0

Jaege
Jaege

Reputation: 1873

The breakpoint is moved down because that line of code is optimized out by the compiler when compile in release mode (i.e /O2). So the compiler will try to find the next line of code where it is not optimized out and move the breakpoint there. I don't know if there is a way to keep the breakpoint from moving down in release mode, but you can disable the breakpoint temporarily by moving your cursor on the red dot and click the toggle button.

Since the line of code marked by breakpoint is optimized out in release mode, there is actually no meaning to break there when running.

See How to: Enable or Disable Breakpoints

Upvotes: 1

Daniel Kovachev
Daniel Kovachev

Reputation: 147

you can do that qhen you compile your code with the flag -Og.
it is imported for you to understund that when you compile your code with optimization the compiler change the order of lines executed to make your code faster.
when you compile with that flag you will not get full optimization but the most without interfere with debugging.

-Og enables optimizations  that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.

check this link: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Upvotes: 0

Related Questions