Visual Studio 2008 中的 if 语句怪异
我遇到了一个非常奇怪的问题,所以我记录了我的会话,因为我认为没有人会相信我。
我遇到了一个似乎非常基本的错误。这是一个单线程应用程序,我所做的就是评估布尔值。
布尔值等于 false,但是 if 语句的执行就好像它是 true 一样......有点像。你就会明白我的意思了。我已经清理了解决方案并重建了很多次。不知道发生了什么事。
我希望得到一些解释。
I've come across a problem so strange, that I've recorded my session because I didn't think anyone would belive me.
I came across a bug that seems to be at very fundamental level. This is a single threaded application and all im doing is evaluating a boolean.
The boolean equals false, however, the if statement is executing as if it were true...sort of. You'll see what I mean. I've cleaned the solution and rebuilt many times. No idea whats going on.
I'd love some explanations please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我过去已经见过很多次了。基本上发生的事情是您正在调试的代码与您看到的代码不匹配。
我不知道是什么原因造成的,解决方案遵循货物崇拜准则。
I have seen that many times in the past. Basically what is happening is that the code you are debugging doesn't match the code you are seeing.
I don't know what causes this and the solution follows cargo-cult guidelines.
我的猜测是部署时发生了一些奇怪的事情,因此 pdb 与实际代码不同步。如果您使用日志记录而不是调试器来弄清楚发生了什么,我怀疑您会看到更明智的行为。我怀疑 CLR 本身的“if”行为很奇怪 - 它更有可能是调试器/运行时不一致。
My guess is that something odd is happening at deployment, so the pdb is out of sync with the actual code. If you use logging instead of the debugger to work out what's going on, I suspect you'll see more sensible behaviour. I doubt that it's the CLR itself behaving weirdly with an "if" - it's much more likely to be a debugger/runtime inconsistency.
我很久以前在 Delphi 中见过类似的情况,所以我的问题是:您是针对发布还是调试进行编译,有或没有优化?
我问这个问题的原因是,有一次,在调试会话期间,我发现了一个由 4-5 行代码组成的小程序,根据调试器的说法,这些代码似乎是反向执行的。
基本上,使用以下类型的代码:
根据调试器的说法,执行顺序是这样的:
原因是这些行之间没有副作用,因此编译器实际上通过重写代码来“优化”代码重新排列代码,使其看起来完全反向执行。
那么,您是否有一个 throw 语句,它实际上是正在执行的语句,但编译器将其显示为您遇到问题的语句,因为,由于重新排列代码,两个 throw 语句实际上只作为可执行文件发出一次代码?
注意:我没有任何理由知道这就是 Visual Studio 正在做的事情,但这是我在看到您的视频时想到的。
I've seen a similar case a long time ago, in Delphi, so my question is this: Are you compiling for Release or Debug, with or without optimizations?
The reason I'm asking is that once, during a debug session, I discovered a small procedure that consisted of 4-5 lines of code that, according to the debugger, appeared to be executing in reverse.
Basically, with the following type of code:
The execution order, according to the debugger, was this:
The reason was that the lines was side-effect free in between themselves, so the compiler "optimized" the code by rewriting it, in effect rearranging the code to appear to execute fully in reverse.
So, do you have a throw statement further down that is actually the one getting executed, but the compiler shows this as the one you have problems with, because, due to rearranging the code, two throw-statements are actually only emitted once as executable code?
Note: I do not have any reason to know that this is what Visual Studio is doing, but this was what came to my mind when seeing your video.
我认为这看起来像是调试步进范围刚刚关闭的情况。您不能总是相信调试器中的黄色突出显示。你实际上并没有介入。在 F# 的早期测试版中,我们有很多这样的错误,其中黄色突出显示会疯狂地跳来跳去。调试器突出显示主要取决于编译器写入 .pdb 文件的内容,作为对应于特定编译指令集的“源范围”。
这是什么版本的 VS/C#?
编辑
看过其他人的答案后,确实可能的原因是您的 .pdb 文件与 .dll 不同步。
I think this looks like a case where the debug stepping ranges are just off. You can't always trust the yellow highlight in the debugger. You're not actually stepping in. In earlier Betas of F# we had lots of bugs like this where the yellow highlight would jump around like crazy. The debugger highlighting is mostly at the mercy of whatever the compiler writes into the .pdb file as the 'source range' that corresponds to a particular compiled set of instructions.
What version of VS/C# is this?
EDIT
Having seen other's answers, indeed a likely cause is that your .pdb file is out of sync with your .dll.
一周前我遇到了完全相同的问题。还有VS2008,最新的SP。 WinForms 应用程序。该值为 false,但
if
正文始终被执行。我正在做与你的视频中相同的调查。这是我的一段代码:在没有编译为“发布”的调试器的情况下运行就可以了。尝试一下。
我认为 VS2008 调试器有一个错误。以某种方式可以用“if”和“throw”关键字重现。
编辑:上面的“执行”一词当然是错误的。必须改用“介入但未执行”。
I had exactly the same problem week ago. Also have VS2008, latest SP. WinForms application. The value was false but
if
body was always executed. I was doing same investigations as in your video. Here is my piece of code:Running without debugger compiled as 'Release' was fine. Try it.
I suppose there is a bug in VS2008 debugger. Somehow reproducible with 'if' and 'throw' keywords.
EDIT: the 'executed' word above is wrong one of course. 'Stepped in but not executed' must be used instead.
只需在代码的时髦突出显示中添加“我也是”即可。我正在用 C# 运行 VS2008。我有一个 Windows 窗体项目引用另一个项目中的类库,并且我正在逐行调试这两个项目。 “在某些时候”,调试中的黄色突出显示距离实际执行的行有 14 到 20 行。
我关闭了 VS,打开了两个项目的目录,删除了两个目录中 bin/Debug 和 obj/Debug 中的所有内容,然后重新启动了 VS。重新编译并单步调试时,一切又恢复正常了。
我不知道问题是否出在 .manifest、.pdb 或者 .Cache 文件中。没关系。把一切都吹走,一切都会好起来的。
FWIW,谷歌搜索几乎没有用,除了它返回了这个 SO 线程。所有其他点击都与 VC++ 模板和 VS2005 的问题有关,其中 SP 修复了该问题。这不是同一个问题。
Just adding a "me too" here on funky highlighting of code. I'm running VS2008 with C#. I have a Windows Forms project referencing a class library in another project and I'm debugging both line by line. "At some point" the yellow highlight in debug was anywhere from 14 to 20 lines off from the actual line being executed.
I closed VS, opened the directories for both projects, deleted everything from bin/Debug and obj/Debug in both directories, then restarted VS. On recompile and stepping through debug, everything was fine again.
I don't know if the issue was in a .manifest, .pdb, or perhaps a .Cache file. It doesn't matter. Blow um all away and it'll be fine.
FWIW, Googling was nearly useless except that it returned this SO thread. All other hits were about issues with VC++ templates and VS2005, where a SP fixed that problem. This is not the same issue.