GCC

GCC Bugs

Article by:

Issue With std::function (and lambdas) With gcc and arm

This specific bug has caused be much pain when cross-compiling C++ code for the Zynq 7020 SoC (which has ARM A9 processors on-board). This caused software to seg fault for not good reason. It appeared when trying to use a captured this inside of a lambda function.

Code To Cause Issue:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  #include #include class App { public: std::function 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; }; App* 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(); } 

The print to std::cout causes the bug to occur. Other, non-stream operations will likely cause a similar issue

Effects: Effects GCC v6.2.0, possibly others.

Workaround: Either compile with only -O0 or -O1 level optimizations, or add the gcc compiler flag -fno-schedule-insns2 (which disables instruction scheduling).

I posted a question on StackOverflow regarding this bug. Phillip Huppertz then found the bug logged in gcc&rsquo;s bugzilla.

Authors

Geoffrey Hunter

Dude making stuff.