c++/2474: global destructors run in the wrong order

lassi.tuura@cern.ch lassi.tuura@cern.ch
Tue Apr 3 03:26:00 GMT 2001


>Number:         2474
>Category:       c++
>Synopsis:       global destructors run in the wrong order
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Apr 03 03:26:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Lassi A. Tuura
>Release:        egcs 1.1.2, gcc 2.95.2
>Organization:
>Environment:
Linux (RH6.1) and Solaris (2.7) at least:
$ c++ -v
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
$ c++ -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
$ c++ -v
Reading specs from /usr/local/gcc-alt-2.95.2/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
>Description:
Function-local static objects get destructed too early.  The
globally constructed objects should be destructed in reverse
constructor *finishing* order.  For objects constructed as
function-local static objects (for a function invoked from
another global constructor), gcc destructs them in in
reverse constructor *starting* order.  This causes a problem
when the corresponding destructor still expects that function-
local static to be valid.
>How-To-Repeat:
$ c++ dtor.c && ./a.out
Y::Y
f()
X::X
main
X::~X
Y::~Y

# The output should be:
Y::Y
f()
X::X
main
Y::~Y
X::~X
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="dtor.cxx"
Content-Disposition: inline; filename="dtor.cxx"

// c++ dtor.cxx && ./a.out
#include <stdio.h>

struct X {
  X (void) { printf("X::X\n"); }
  ~X (void) { printf("X::~X\n"); }
};

X &f (void) {
  printf ("f()\n");
  static X x;
  return x;
}

struct Y {
  Y (void) { printf("Y::Y\n"); f(); }
  ~Y (void) { printf("Y::~Y\n"); }
};

Y y;

int main (int, char **) {
  fprintf(stdout, "main\n");
  return 0;
}



More information about the Gcc-bugs mailing list