The following program - compiled with flags -Wall -pedantic-errors produces an unexpected output: //----------------------------------- extern "C" int printf(const char *, ...); class A { A(const A&); A& operator=(const A&); A() { printf("A()\n"); } ~A() { printf("~A()\n"); } public: void use() { printf("A is here!\n"); } static A& get_instance() { static A result; return result; } }; void use_A(const char* message) { A& a = A::get_instance(); printf("Using A %s\n", message); a.use(); } struct B { ~B() { use_A("from ~B()"); } } b; int main() {} //----------------------------------- A() Using A from ~B() A is here! Note the lack of the expected last line: ~A() Some further characteristics: 1) The problem is observed on a mingw-64bit system (Windows 7). I have been told that the problem doesn't occur on Linux(?) systems (Thanks to Jonathan Wakely) 2) The kind of global destruction semantics doesn't matter: Instead of a local static variables we can use corresponding series of nested atexit() registrations. 3) The problem is not related to flushing: Replacement of above printf calls by e.g. std::cout with an effective std::flush call doesn't change anything
Added Kai hoping to raise his interest in this issue ;-)
Let's add Jason in CC. Confirmed that doesn't happen on Linux.
This is IIRC the whole atexit vs __cxa_atexit issue.
(In reply to Andrew Pinski from comment #3) > This is IIRC the whole atexit vs __cxa_atexit issue. Which is PR 2474 .