This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Obscure iostream failure, a compiler bug or a language design issue?


I had a C++ program that crashed, so I added debug output using clog. Unfortunately the first out put to clog also crashed within iostreams.
I was able to make a test case that compiles fine with -Wall -Wextra, but crashes:
---ugly code follows---
class d {
        static void f();
        d() { f(); }

d dd;

#include        <iostream>
using namespace std;
void d::f() { clog << "jdfdf" << endl; }
int main(int argc, char *argv[])

If you #include <iostream> before the decalration of "dd", the program works; if compiled as displayed, it crashes:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b6f501 in std::ostream::sentry::sentry(std::ostream&) ()
   from /usr/lib64/
(gdb) bt
#0  0x00007ffff7b6f501 in std::ostream::sentry::sentry(std::ostream&) ()
   from /usr/lib64/
#1  0x00007ffff7b6fc19 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /usr/lib64/
#2  0x00007ffff7b7001f in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib64/
#3  0x0000000000400942 in d::f () at
#4  0x0000000000400a6a in d::d (this=0x6011a0 <dd>) at
#5  0x00000000004009d9 in __static_initialization_and_destruction_0 (
    __initialize_p=1, __priority=65535) at
#6  0x0000000000400a33 in global constructors keyed to dd() () at
#7  0x0000000000400b46 in __do_global_ctors_aux ()
#8  0x0000000000400783 in _init ()
#9  0x00007ffff72dcad8 in ?? () from /lib64/
#10 0x0000000000400ad5 in __libc_csu_init (argc=-7936,
    argv=0x601080 <_ZSt4clog@@GLIBCXX_3.4>, envp=0x0) at elf-init.c:120
#11 0x00007ffff72ebbc2 in __libc_start_main () from /lib64/
#12 0x0000000000400859 in _start () at ../sysdeps/x86_64/elf/start.S:113

gcc is "gcc version 4.3.4 [gcc-4_3-branch revision 152973] (SUSE Linux" of SLES11 SP3 (x86_64), but newer compilers react the same.

I guess the problem is that constructors are called sequentially without dependency analysis. So if d() is acaaled before including iostream (lexically), the program crashes.

Ulrich Windl
P.S. Usually I don't write such ugly code

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]