Bug 56009 - Nested global destruction semantics not working (mingw)
Summary: Nested global destruction semantics not working (mingw)
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-01-16 19:58 UTC by Daniel Krügler
Modified: 2024-05-04 07:35 UTC (History)
2 users (show)

See Also:
Host:
Target: mingw-64bit
Build:
Known to work:
Known to fail: 4.7.2, 4.8.0
Last reconfirmed: 2013-01-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Krügler 2013-01-16 19:58:01 UTC
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
Comment 1 Daniel Krügler 2013-01-21 10:45:58 UTC
Added Kai hoping to raise his interest in this issue ;-)
Comment 2 Paolo Carlini 2013-01-21 11:03:20 UTC
Let's add Jason in CC. Confirmed that doesn't happen on Linux.
Comment 3 Andrew Pinski 2024-04-09 02:35:04 UTC
This is IIRC the whole atexit vs __cxa_atexit issue.
Comment 4 Andrew Pinski 2024-04-09 02:38:52 UTC
(In reply to Andrew Pinski from comment #3)
> This is IIRC the whole atexit vs __cxa_atexit issue.

Which is PR 2474 .