“这个"由 lambda 捕获是不正确的.GCC编译器错误?
问题描述
在过去的几天里,我一直在调试一个涉及 C++ 中 lambda 的奇怪问题.我已将问题归结为以下症状:
For the last few days, I have been debugging a weird issue involving lambdas in C++. I have reduced the problem down to the following symptoms:
this
指针在 lambda 中损坏(注意:this
总是被副本捕获,因此 lambda 应该有自己的this
指针,指向App
对象)- 只有在
std::cout
打印语句存在时才会发生,并且在创建 lambda 之前调用.print 语句可能看起来完全不相关(例如 print "Hello!").printf()
也表现出相同的行为. - 仅在交叉编译时发生.
- 使用
x86
架构的标准编译器可以正常编译和运行(参见 example). - 如果我在堆上创建 lambda(并将指向它的指针保存在
App
对象中),则不会发生错误. - 如果关闭优化(即如果我设置了
-O0
标志),则不会出现该错误.当优化设置为-O2
时发生.
- The
this
pointer gets corrupted inside a lambda (note:this
is always captured by copy, so the lambda should get its ownthis
pointer, which points to theApp
object) - It only occurs if a
std::cout
print statement is present, and called before the lambda is created. The print statement can be seemingly completely unrelated (e.g. print "Hello!").printf()
also exhibits the same behaviour. - It only occurs when cross-compiling.
- It compiles and runs fine with the standard compiler for
x86
architecture (see example). - If I create the lambda on the heap (and save a pointer to it inside the
App
object), the bug does not occur. - The bug does not occur if optimizations are turned off (i.e. if I set the
-O0
flag). It occurs when optimization is set to-O2
.
以下是我能想到的导致问题的最简单、可编译的代码示例.
The following is the simplest, compilable code example I could come up with that causes the problem.
在目标设备上运行时,我得到以下输出:
When running on the target device, I get the following output:
如果我尝试取消引用损坏的 this
,我通常会遇到分段错误,这就是我最初发现错误的方式.
If I try and dereference the corrupted this
, I usually get a segmentation fault, which is how I discovered the bug in the first place.
链接器设置
编译器版本
这可能是编译器错误吗?
Could this be a compiler bug?
推荐答案
这似乎是 gcc 6.2 中的编译器错误,见:
This seems to be a compiler bug in gcc 6.2, see:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77686
解决方法:
- 使用
-fno-schedule-insns2
标志(正如 gbmhunter 所指出的,请参阅下面的评论). - 不要使用
-O2
或更高的优化.
- Use
-fno-schedule-insns2
flag (as pointed out by gbmhunter, see comment below). - Do not use
-O2
optimizations or higher.
这篇关于“这个"由 lambda 捕获是不正确的.GCC编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!