The following code crashes during initialization of globals. Has to be compiled by "recent" gcc snapshot - this problem first appeared sometimes between 20090611 and 20090618. ---- header.h ---------- struct A { static int &Init() { static int &i = *new int(); return i; } A() { A::Init(); } }; ---- main.cpp ---------- #include "header.h" static A a; int main(int argc, char *argv[]) { return 0; } ---- file.cpp ---------- #include "header.h" ------------------ $ g++ file.cpp main.cpp -o a.out && ./a.out Segmentation Fault (SIGSEGV) Attached: preprocessed files, "g++ file.ii main.ii -o a.out && ./a.out" to reproduce the problem. The order of files passed as parameters is important. It should be valid C++ with defined behaviour, but I am not sure about that, so I am asking here instead of opening bug report. (note that simple "static int i; return i;" doesn't have defined behaviour for non-primitive types (afaik))
# 1 "file.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "file.cpp" # 1 "header.h" 1 struct A { static int &Init() { static int &i = *new int(); return i; } A() { A::Init(); } }; # 1 "file.cpp" 2
# 1 "main.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "main.cpp" # 1 "header.h" 1 struct A { static int &Init() { static int &i = *new int(); return i; } A() { A::Init(); } }; # 2 "main.cpp" 2 static A a; int main(int argc, char *argv[]) { return 0; }