This is the mail archive of the gcc-bugs@gcc.gnu.org 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]

Bug in destructor ordering?


Using egcs-1.1.2, I am seeing a problem with destructor ordering on an
Intel RedHat 6.1 Linux -- but not on RedHat 5.1.  Basically,
function-local static variable seems to get destructed too early.  An
example problem is below, first with the output I expected, and then the
wrong behaviour (X::~X gets invoked too early).  Does anybody have any
idea if this is a bug in the compiler or elsewhere in the system
(glibc?).  If in the compiler, can anyone verify it has it been fixed in
later versions such 2.95.x?

Any suggestions for a solution or a work-around would be greatly
appreciated!  And while I am at it, thank you so much for the wonderful
tools!

//lat
-- 
Old and young, we are all on our last
cruise.  --Robert Louis Stevenson

# Expected output
$ 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)
$ rpm -qa | grep glibc
glibc-2.0.7-19
glibc-debug-2.0.7-19
glibc-devel-2.0.7-19
$ c++ dtor.cxx && ./a.out
Y::Y
f()
X::X
main
Y::~Y
f()
X::~X

# Incorrect output
$ 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)
$ rpm -qa | grep glibc
glibc-2.1.2-11
glibc-devel-2.1.2-11
$ c++ dtor.cxx && ./a.out
Y::Y
f()
X::X
main
X::~X
Y::~Y
f()

$ cat dtor.cxx 
#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"); f(); }
};

Y y;

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

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