This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
bad ctor/dtor call ordering with g++ 2.96 20000229 (linux/ia86)
- To: bug-gcc at gnu dot org
- Subject: bad ctor/dtor call ordering with g++ 2.96 20000229 (linux/ia86)
- From: scott snyder <snyder at fnal dot gov>
- Date: Wed, 01 Mar 2000 21:40:41 CST
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