Bug 18

Summary: bad ctor/dtor call ordering
Product: gcc Reporter: martin
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs, loewis, nathan, neil
Priority: P3 Keywords: wrong-code
Version: 2.96   
Target Milestone: ---   
Host: i586-pc-linux-gnu Target: i586-pc-linux-gnu
Build: i586-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed:

Description martin 2000-03-02 13:06:00 UTC
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

Release:
2.96 20000229

Environment:
System: Linux mira 2.3.48 #4 Sun Feb 27 23:26:02 CET 2000 i586 unknown
Architecture: i586

	
host: i586-pc-linux-gnu
build: i586-pc-linux-gnu
target: i586-pc-linux-gnu
Comment 1 martin 2000-03-02 13:06:00 UTC
Fix:
[nathan] 
The current CVS [20000626] produces on i686-pc-linux-gnu
ctor bffff650
ctor bffff640
dtor bffff640
dtor bffff650

Please confirm whether the bug is fixed, or more information#
if not.
Comment 2 Martin v. Loewis 2000-03-08 23:12:44 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed as a bug
Comment 3 Martin v. Loewis 2000-03-09 07:12:44 UTC
From: loewis@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org, martin@loewis.home.cs.tu-berlin.de,
  nobody@gcc.gnu.org, snyder@fnal.gov
Cc:  
Subject: Re: c++/18
Date: 9 Mar 2000 07:12:44 -0000

 Old Synopsis: [bad code] bad ctor/dtor call ordering
 New Synopsis: bad ctor/dtor call ordering
 
 State-Changed-From-To: open->analyzed
 State-Changed-By: loewis
 State-Changed-When: Wed Mar  8 23:12:44 2000
 State-Changed-Why:
     Confirmed as a bug
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=18&database=gcc
Comment 4 Nathan Sidwell 2000-06-26 08:56:59 UTC
State-Changed-From-To: analyzed->feedback
State-Changed-Why: Appears fixed
Comment 5 Nathan Sidwell 2000-06-26 15:56:59 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org, martin@loewis.home.cs.tu-berlin.de,
  nobody@gcc.gnu.org, snyder@fnal.gov
Cc:  
Subject: Re: c++/18
Date: 26 Jun 2000 15:56:59 -0000

 Synopsis: bad ctor/dtor call ordering
 
 State-Changed-From-To: analyzed->feedback
 State-Changed-By: nathan
 State-Changed-When: Mon Jun 26 08:56:59 2000
 State-Changed-Why:
     Appears fixed
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=18&database=gcc
Comment 6 Neil Booth 2001-01-13 14:43:18 UTC
State-Changed-From-To: feedback->closed
State-Changed-Why: Fixed in CVS.
Comment 7 Neil Booth 2001-01-13 22:43:18 UTC
From: neil@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org, martin@loewis.home.cs.tu-berlin.de,
  nobody@gcc.gnu.org, snyder@fnal.gov
Cc:  
Subject: Re: c++/18
Date: 13 Jan 2001 22:43:18 -0000

 Synopsis: bad ctor/dtor call ordering
 
 State-Changed-From-To: feedback->closed
 State-Changed-By: neil
 State-Changed-When: Sat Jan 13 14:43:18 2001
 State-Changed-Why:
     Fixed in CVS.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=18&database=gcc