[英]Program crashes without debug symbols, runs fine with them (no optimizations or anything in either case)
I've come across a very baffling crash and having trouble tracing it in my program. 我遇到了一个非常令人困惑的崩溃,并且无法在我的程序中跟踪它。
I have no idea where its source is, because whenever I attempt to link with the /debug
flag in order to debug with debug symbols, the crash disappears! 我不知道它的来源在哪里,因为每当我尝试与
/debug
标志链接以便使用调试符号进行调试时,崩溃就会消失!
This seems to be independent of optimizations (I've compiled with /Od
to disable compiler optimizations and I'm not using link-time code generation) and also independent of debug information itself (I'm compiling with /Zi
). 这似乎与优化无关(我用
/Od
编译来禁用编译器优化而我没有使用链接时代码生成)并且也独立于调试信息本身(我用/Zi
编译)。 For some reason, when the linker is called without the /debug
flag, everything behaves differently. 出于某种原因,在没有
/debug
标志的情况下调用链接器时,一切都表现不同。
What could possibly be causing this? 什么可能导致这个?
It turned out the culpirit was merging of static const int
fields by the linker. 事实证明,culpirit正在通过链接器合并
static const int
字段 。
Apparently, if you have multiple static const
fields of the same type and value, the linker will merge them, but only if you don't link debug symbols. 显然,如果您有多个相同类型和值的
static const
字段,链接器将合并它们,但前提是您不链接调试符号。
I was using the addresses of such fields to distinguish between them, and as it turned out they all looked the same to the linker, and hence to the rest of the code. 我使用这些字段的地址来区分它们,结果看起来它们对链接器看起来都是一样的,因此对代码的其余部分也是如此。
How did I figure this out? 我怎么知道这个?
Lots and lots of print
statements (hours) led me to realize that the wrong code is being executed, until I finally realized the cause. 大量的
print
语句(小时)让我意识到错误的代码正在执行,直到我终于意识到原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.