简体   繁体   English

调试优化的版本会导致程序的行为有所不同吗?

[英]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. 由于SDK的古怪之处,我只能将我的插件构建为Release版本。

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. 在调试器中逐步进行调试时,我发现似乎是堆损坏以及SDK函数中的访问冲突,但是代码似乎没有任何问题。 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. Dito用于更改程序运行的环境,而“在调试器下”则是完全不同的环境。
That especially impacts race-conditions, which are hard to diagnose using a debugger. 这尤其会影响竞态条件,而竞态条件很难使用调试器进行诊断。
See heisenbug . 参见heisenbug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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