简体   繁体   中英

Can debugging an optimized build cause a program to behave differently?

I am working on a plugin for an application. Due to a quirk of the SDK, I can only build my plugin as a Release build.

While working on a particular portion of my code, I have found odd behaviour. When stepping through it in the debugger, I get what appears to be heap corruption, and access violations within the SDKs functions, but there does not appear to be anything wrong with the code. The code runs fine outside the debugger.

Most importantly, if I turn off optimisation, I can step through it fine.

I know that I should not debug optimised code, but always thought it was because the compiler did things like inline functions, unroll or remove redundant loops and optimise away local variables. The debugger would have reduced visibility of what was going on yes, but it wouldn't break anything.

This makes me concerned that turning off optimisations is just hiding a bug. So my question is, should I be expecting to step through an optimised build like a debug build, or should I expect the debugger to break it?

Well, there are two questions:

  1. Does turning off optimization hide bugs?
  2. Does using a debugger break things?

The answer to both is sometimes.

Any change to build-options potentially both hides and exposes a different set of bugs, as well as altering how they are expressed.

Dito for changing the environment in which your program runs, and "under the debugger" is a completely different environment, than without.
That especially impacts race-conditions, which are hard to diagnose using a debugger.
See heisenbug .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM