https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67791
Bug ID: 67791 Summary: Crash using std::thread and iostream with dynamic loading of a shared library Product: gcc Version: 5.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: nexyon at gmail dot com Target Milestone: --- Created attachment 36430 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36430&action=edit the source of the shared library If you think the summary of this bug is long/strange: yes, to me this seems like the most weird bug, I have ever found. What I am doing is the following: * I have a main program (main.cpp) that loads a dynamic library thread.so and calls a function in there. * main.cpp also includes <iostream> without actually using this library. (sounds weird, eh?) * The function in thread.so creates a std::thread and runs it. * The application crashes when the thread should be started. Let me show you what happens (or: how to reproduce): -------------- % g++ -g -fPIC -shared -o thread.so thread.cpp -std=c++11 -lpthread % g++ -g -ldl -o main main.cpp % gdb main GNU gdb (GDB) 7.10 Copyright (C) 2015 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-unknown-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from main...done. (gdb) r Starting program: /data/Work/Computer/Programming/Audaspace/bug/main [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/libthread_db.so.1". Program received signal SIGSEGV, Segmentation fault. 0x0000000000000000 in ?? () (gdb) bt full #0 0x0000000000000000 in ?? () No symbol table info available. #1 0x00007ffff790e493 in __gthread_create (__args=<optimized out>, __func=0x7ffff790e330 <std::(anonymous namespace)::execute_native_thread_routine(void*)>, __threadid=0x7fffffffdd60) at /build/gcc-multilib/src/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu/bits/gthr-default.h:662 No locals. #2 std::thread::_M_start_thread (this=0x7fffffffdd60, __b=...) at /build/gcc-multilib/src/gcc-5.2.0/libstdc++-v3/src/c++11/thread.cc:149 No locals. #3 0x00007ffff6d99826 in std::thread::thread<void (&)(int, unsigned long long*), int, unsigned long long*&>(void (&)(int, unsigned long long*), int&&, unsigned long long*&) (this=0x7fffffffdd60, __f= @0x7ffff6d993e2: {void (int, unsigned long long *)} 0x7ffff6d993e2 <factorial(int, unsigned long long*)>) at /usr/include/c++/5.2.0/thread:137 No locals. #4 0x00007ffff6d99463 in run () at thread.cpp:17 a = 4196233 c = 0x7fffffffdd70 t1 = {_M_id = {_M_thread = 0}} #5 0x0000000000400793 in main (argc=1, argv=0x7fffffffdea8) at main.cpp:9 handle = 0x612c40 run_func = 0x7ffff6d99430 <run()> (gdb) -------------- Now why is this weird/funny? There are two ways you can get rid of the crash: * the first is to add -lpthread to the second g++ line (the one that compiles main.cpp and links the main program) OR * you remove the line #include <iostream> in main.cpp I found this bug in a way bigger code base as you can imagine and I already spent a lot of time to get it down to this minimal example. I hope you can find, what is the problem here! Thanks!