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]

bad ctor/dtor call ordering with g++ 2.96 20000229 (linux/ia86)



hi -

The CVS version of g++ (2.96 20000229)  on a i686-pc-linux-gnu platform
generates invalid code for the following input:

-- egcsbug8.cc ---------------------------------------------------------
extern "C" int printf (...);

class allocator {
public:
  allocator()  {}
  allocator(const allocator&)  {}
  ~allocator()  {}
};

class basic_string
{
public:
  struct _Alloc_hider : allocator {};
  _Alloc_hider _M_dataplus;
};


class d0_String
  : public basic_string
{
public:
  d0_String (const char* s) { printf ("ctor %x\n", this); }
  ~d0_String () { printf ("dtor %x\n", this); }
};


struct pair {
  pair(const d0_String& __a) {}
};



int main ()
{
  pair pp[] = {
    pair (d0_String ("i")),
    pair (d0_String ("j"))
  };
  return 0;
}
------------------------------------------------------------------------

The code in main() will create two temporaries of type `d0_String' and
then destroy them.  I put in code to trace the constructor and destructor
calls; what i would expect to see is something like

  ctor A
  dtor A
  ctor A
  dtor A

where A is some address, or possibly

  ctor A
  ctor B
  dtor A
  dtor B

where A and B are different addresses.  Instead, here's what i see:

$ g++ -o egcsbug8 egcsbug8.cc
$ ./egcsbug8
ctor bfffe8f0
ctor bfffe8f0
dtor bfffe8f0
dtor bfffe8f0

First, the constructor is called twice, _on the same object_.
Then, the destructor is called twice, also on the same object.
Here's the section of the generated code which calls the destructors.
One can see that the destructor is explicitly being called twice
for the same stack slot:

.LEHE41:
	subl	$8, %esp
	pushl	$2
	leal	-8(%ebp), %eax
	subl	$32, %eax
	pushl	%eax
	call	_._9d0_String
	addl	$16, %esp
.LEHE40:
	subl	$8, %esp
	pushl	$2
	leal	-8(%ebp), %eax
	subl	$32, %eax
	pushl	%eax
	call	_._9d0_String
	addl	$16, %esp
	movl	$0, %eax
	jmp	.L39


thanks,
sss

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