https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117365
Bug ID: 117365 Summary: Captured this lost after assignment to std::function Product: gcc Version: 6.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: Koen.Poppe at vandewiele dot com Target Milestone: --- Created attachment 59498 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59498&action=edit Preprocessed source of minimal example When cross compiling to ARM: a lambda with "this" captured stored into an std::function looses the by-value captured this which results in a segmentation fault if that this pointer is used. Minimal program (main.cpp, preprocessed source attached): #include <functional> #include <iostream> #include <inttypes.h> #define CHECK(cond) do { if (cond){ std::cout << __LINE__ << ": '" #cond "' = " << "ok" << std::endl; } else { std::cerr << __LINE__ << ": '" #cond "' FAILED!" << std::endl; return __LINE__; } } while(false) struct Class { using Fun = std::function<Class*()>; int testWrapped() { Fun fun; CHECK(!fun); std::cout << "this = " << reinterpret_cast<intptr_t>(this) << std::endl; fun = [this]() -> Class* { std::cout << "this captured in lambda = " << reinterpret_cast<intptr_t>(this) << std::endl; return this; }; CHECK(fun); CHECK(fun() == this); std::cout << "lambda returns" << reinterpret_cast<intptr_t>(fun()); return 0; } }; int main() { Class c; return c.testWrapped(); } Expected output of the attached program: 13: '!fun' = ok this = 2127567732 19: 'fun' = ok this captured in lambda = 2127567732 20: 'fun() == this' = ok Actual output: 13: '!fun' = ok this = 2127567732 19: 'fun' = ok this captured in lambda = 135996 20: 'fun() == this' FAILED! As this is compiling for an embedded target, I have not been able to run this with a more recent version to check whether this is already fixed. Output of gcc -v -save-temps: compiling main.cpp Using built-in specs. COLLECT_GCC=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ Target: arm-poky-linux-gnueabi Configured with: ../../../../../../work-shared/gcc-6.2.0-r0/gcc-6.2.0/configure --build=x86_64-linux --host=x86_64-pokysdk-linux --target=arm-poky-linux-gnueabi --prefix=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr --exec_prefix=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr --bindir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi --sbindir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi --libexecdir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi --datadir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/share --sysconfdir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/etc --sharedstatedir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/com --localstatedir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/var --libdir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi --includedir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/include --oldincludedir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/include --infodir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/share/info --mandir=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/share/man --disable-silent-rules --disable-dependency-tracking --with-libtool-sysroot=/tmp/work/tmp/sysroots/x86_64-nativesdk-pokysdk-linux --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads=posix --enable-multilib --enable-c99 --enable-long-long --enable-symvers=gnu --enable-libstdcxx-pch --program-prefix=arm-poky-linux-gnueabi- --without-local-prefix --enable-lto --enable-libssp --enable-libitm --disable-bootstrap --disable-libmudflap --with-system-zlib --with-linker-hash-style=gnu --enable-linker-build-id --with-ppl=no --with-cloog=no --enable-checking=release --enable-cheaders=c_global --without-isl --with-gxx-include-dir=/not/exist/usr/include/c++/6.2.0 --with-build-time-tools=/tmp/work/tmp/sysroots/x86_64-linux/usr/arm-poky-linux-gnueabi/bin --with-sysroot=/not/exist --with-build-sysroot=/tmp/work/tmp/sysroots/imx6sololcc --without-long-double-128 --enable-poison-system-directories --with-mpfr=/tmp/work/tmp/sysroots/x86_64-nativesdk-pokysdk-linux --with-mpc=/tmp/work/tmp/sysroots/x86_64-nativesdk-pokysdk-linux --enable-nls --enable-initfini-array --with-arch=armv7-a Thread model: posix gcc version 6.2.0 (GCC) COLLECT_GCC_OPTIONS='--sysroot=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi' '-march=armv7-a' '-mfpu=neon' '-mfloat-abi=hard' '-mcpu=cortex-a9' '-c' '-O2' '-g' '-feliminate-unused-debug-types' '-Werror' '-fvisibility=hidden' '-fvisibility-inlines-hidden' '-O2' '-v' '-save-temps' '-std=gnu++11' '-Wall' '-Wextra' '-D' '_REENTRANT' '-fPIC' '-D' 'NDEBUG' '-D' '-I' '.' '-o' 'main.o' '-shared-libgcc' '-mtls-dialect=gnu' /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/cc1plus -E -quiet -v -I . -I tmp/release -isysroot /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi -D_GNU_SOURCE -D _REENTRANT main.cpp -march=armv7-a -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 -mtls-dialect=gnu -std=gnu++11 -Werror -Wall -Wextra -feliminate-unused-debug-types -fvisibility=hidden -fvisibility-inlines-hidden -fPIC -g -fworking-directory -O2 -O2 -fpch-preprocess -o main.ii ignoring nonexistent directory "/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/local/include" ignoring nonexistent directory "/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/../../../../../arm-poky-linux-gnueabi/include" #include "..." search starts here: #include <...> search starts here: . /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/c++/6.2.0 /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/c++/6.2.0/arm-poky-linux-gnueabi /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include/c++/6.2.0/backward /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/include /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/lib/gcc/arm-poky-linux-gnueabi/6.2.0/include /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/include-fixed /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/include End of search list. COLLECT_GCC_OPTIONS='--sysroot=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi' '-march=armv7-a' '-mfpu=neon' '-mfloat-abi=hard' '-mcpu=cortex-a9' '-c' '-O2' '-g' '-feliminate-unused-debug-types' '-Werror' '-fvisibility=hidden' '-fvisibility-inlines-hidden' '-O2' '-v' '-save-temps' '-std=gnu++11' '-Wall' '-Wextra' '-D' '_REENTRANT' '-fPIC' '-I' '.' '-I' 'tmp/release' '-o' 'tmp/release/main.o' '-shared-libgcc' '-mtls-dialect=gnu' /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cpp -march=armv7-a -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 -mtls-dialect=gnu -auxbase-strip tmp/release/main.o -g -O2 -O2 -Werror -Wall -Wextra -std=gnu++11 -version -feliminate-unused-debug-types -fvisibility=hidden -fvisibility-inlines-hidden -fPIC -o main.s GNU C++11 (GCC) version 6.2.0 (arm-poky-linux-gnueabi) compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.4, MPC version 1.0.3, isl version none GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C++11 (GCC) version 6.2.0 (arm-poky-linux-gnueabi) compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.4, MPC version 1.0.3, isl version none GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 22642f806ec0c662250f0cdd3c15f78b COLLECT_GCC_OPTIONS='--sysroot=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi' '-march=armv7-a' '-mfpu=neon' '-mfloat-abi=hard' '-mcpu=cortex-a9' '-c' '-O2' '-g' '-feliminate-unused-debug-types' '-Werror' '-fvisibility=hidden' '-fvisibility-inlines-hidden' '-O2' '-v' '-save-temps' '-std=gnu++11' '-Wall' '-Wextra' '-D' '_REENTRANT' '-fPIC' '-I' '.' '-I' 'tmp/release' '-o' 'tmp/release/main.o' '-shared-libgcc' '-mtls-dialect=gnu' /opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/as -v -I . -I tmp/release -march=armv7-a -mcpu=cortex-a9 -mfloat-abi=hard -mfpu=neon -meabi=5 -o tmp/release/main.o main.s GNU assembler version 2.27.0 (arm-poky-linux-gnueabi) using BFD version (GNU Binutils) 2.27.0.20160806 COMPILER_PATH=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/ LIBRARY_PATH=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/lib/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/lib/arm-poky-linux-gnueabi/6.2.0/:/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/lib/ COLLECT_GCC_OPTIONS='--sysroot=/opt/fsl-imx-x11/4.9.11-1.0.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi' '-march=armv7-a' '-mfpu=neon' '-mfloat-abi=hard' '-mcpu=cortex-a9' '-c' '-O2' '-g' '-feliminate-unused-debug-types' '-Werror' '-fvisibility=hidden' '-fvisibility-inlines-hidden' '-O2' '-v' '-save-temps' '-std=gnu++11' '-Wall' '-Wextra' '-D' '_REENTRANT' '-fPIC' '-I' '.' '-I' 'tmp/release' '-o' 'tmp/release/main.o' '-shared-libgcc' '-mtls-dialect=gnu'