“此"由lambda捕获的不正确. GCC编译器错误? [英] "this" captured by lambda is incorrect. GCC compiler bug?

查看:95
本文介绍了“此"由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指针,该指针指向对象)
  • 它仅在存在std::cout打印语句时发生,并在创建lambda之前调用. print声明看似完全不相关(例如,打印"Hello!"). printf()也表现出相同的行为.
  • 它仅在交叉编译时发生.
  • 它可以使用x86体系结构的标准编译器编译并运行良好(请参见示例).
  • 如果我在堆上创建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 own this pointer, which points to the App 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.

#include <iostream>
#include <functional>

class App {

public:

    std::function<void*()> test_;

    void Run() {

        // Enable this line, ERROR is printed
        // Disable this line, app runs o.k.
        std::cout << "This print statement causes the bug below!" << std::endl;

        test_ = [this] () {
            return this;
        };

        void* returnedThis = test_();
        if(returnedThis != this) {
            std::cout << "ERROR: 'this' returned from lambda (" << returnedThis 
                      << ") is NOT the same as 'this' (" << this << ") !?!?!?!?!"
                      << std::endl;
        } else {
            std::cout << "Program run successfully." << std::endl;
        }

    }
};

int main(void) {
    App app;
    app.Run();
}

在目标设备上运行时,我得到以下输出:

When running on the target device, I get the following output:

This print statement causes the bug below!
ERROR: 'this' returned from lambda (0xbec92dd4) is NOT the same as 'this' 
(0xbec92c68) !?!?!?!?!

如果我尝试取消引用损坏的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.

arm-poky-linux-gnueabi-g++ -march=armv7-a -marm -mfpu=neon -std=c++14 \
-mfloat-abi=hard -mcpu=cortex-a9 \
--sysroot=/home/ghunter/sysroots/cortexa9hf-neon-poky-linux-gnueabi \
-O2 -pipe -g -feliminate-unused-debug-types

链接器设置

arm-poky-linux-gnueabi-ld \
--sysroot=/home/ghunter/sysroots/cortexa9hf-neon-poky-linux-gnueabi \
-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed

编译器版本

~$ arm-poky-linux-gnueabi-g++ --version

arm-poky-linux-gnueabi-g++ (GCC) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

这可能是编译器错误吗?

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编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆