c++/523: static object never destructed

Benjamin Kosnik bkoz@redhat.com
Mon Sep 11 13:39:00 GMT 2000


Hmm. I can reproduce this with both gcc-2.95.2 and current gcc. I've
abstracted out your testcase so that iostreams are not necessary. 

New code that demonstrates this is appended, below.

It looks like a non-templatized struct will result in ~A being called,
but the same class as a template will not call ~A: very strange.


ie:

using class B, no C:

setting a breakpoint in ~A gives:

Breakpoint 2, A::~A (this=0x0, __in_chrg=65535) at list02.cc:23
(gdb) where
#0  A::~A (this=0x0, __in_chrg=65535) at list02.cc:23
#1  0x804863d in global destructors keyed to main () at list02.cc:21
#2  0x80484dc in __do_global_dtors_aux ()
#3  0x8048771 in _fini ()
#4  0x400b0f56 in ?? ()
#5  0x4009db6e in ?? ()

Using the non-templatized C class for B, ~A does not get hit.






Looking at the C-case assembly for main:

	movl	%ebp, %esp
	popl	%ebp
	ret
.LFE1:
.Lfe1:
	.size	 main,.Lfe1-main
	.section	.gnu.linkonce.t.__t1C1Zi,"ax",@progbits
	.align 16
	.weak	__t1C1Zi
	.type	 __t1C1Zi,@function


And B:

	movl	%ebp, %esp
	popl	%ebp
	ret
.LFE1:
.Lfe1:
	.size	 main,.Lfe1-main
	.local	__ioinit
	.comm	__ioinit,4,4
.globl _1B.a
.bss
	.type	 _1B.a,@object
	.size	 _1B.a,1
_1B.a:
	.zero	1
.text
	.align 16
	.type	 __static_initialization_and_destruction_0,@function
__static_initialization_and_destruction_0:



Here's some sample code:

// start
class ios_base
{
public:
  class Init
  {
    friend class ios_base;
    int i;
  public:
    Init(int j = 0): i(j) { };
    ~Init() { };
    void inc_i(int arg) { i += arg; }
    void dec_i(int arg) { i -= arg; }
    int get_i() { return i; }
  };
};

static ios_base::Init __ioinit;

struct A 
{
  void f(){};
  A(){ __ioinit.inc_i(1); }
  ~A(){ __ioinit.dec_i(1); }
};

// non-templatized
#if 1
struct B 
{
  B(){ a.f(); }
  static A a;
};

A B::a;


// templatized
#else
template<class T>
struct C
{
  C(){ a.f(); }
  static A a;
};

template <class T> A C<T>::a;
typedef C<int> B;
#endif

int main()
{
  B b;
  return 0;
}
//end




Anyway. I'll add this regression for libstdc++-v3's iostreams.

Jason?

-benjamin





More information about the Gcc-bugs mailing list