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]
Other format: [Raw text]

[Bug tree-optimization/16115] New: 3.5: problem with argument passing via temporary (breaks auto_ptr)



The code y1.cc below seems to produce the wrong result if it is compiled
with -O2.  (It works ok, however, with either -O1 or -O3.)


$ g++ -O2 -o y1 y1.cc
$ ./y1
a bfffb690
d 0 0
dtor 1 bfffb690
$

In the last line of output, i expect the value of a printed by the destructor
to be 0; however, it is still 1.


Here is the generated code for the foo() and main() functions (passed
through c++filt, omitting exception handling labels and cleanups):

foo(auto_ptr):
	pushl	%ebp
	xorl	%edx, %edx
	movl	%esp, %ebp
	xorl	%eax, %eax
	subl	$24, %esp
	movl	%edx, 8(%esp)
	movl	%eax, 4(%esp)
	movl	$.LC0, (%esp)
	call	printf
	leave
	ret


main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$40, %esp
	andl	$-16, %esp
	subl	$16, %esp
	movl	%esi, -4(%ebp)
	leal	-24(%ebp), %esi
	movl	%ebx, -8(%ebp)
	movl	$1, -24(%ebp)
	movl	%esi, 4(%esp)
	movl	$.LC1, (%esp)
	call	printf
	movl	%esi, (%esp)
	call	foo(auto_ptr)
	movl	-24(%ebp), %eax
	movl	%esi, 8(%esp)
	movl	$.LC2, (%esp)
	movl	%eax, 4(%esp)
	call	printf
	movl	-8(%ebp), %ebx
	xorl	%eax, %eax
	movl	-4(%ebp), %esi
	movl	%ebp, %esp
	popl	%ebp
	ret
...
.LC0:
	.string	"d %x %x\n"
.LC1:
	.string	"a %x\n"
.LC2:
	.string	"dtor %x %x\n"


It looks like something is going wrong with handling the temporary object
that gets passed to foo().

With gcc 3.4, this code works as expected, so this is a regression.


FYI, here is an earlier stage in the reduction of the test case, with
the auto_ptr class left intact.  Here, the problem shows up as a double
deletion of the allocated object.  Again, it shows up at -O2, but not
at -O1 or -O3.

$ g++ -O2 -o y y.cc
$ ./y
a ctor 804a008
a dtor 804a008
a dtor 804a008
$


y.cc -------------------------------------------
#include <memory>

struct A
{
  A() { printf ("a ctor %x\n", this); }
  ~A() { printf ("a dtor %x\n", this); }
};


void foo (std::auto_ptr<A> a)
{
  delete a.release();
}


int main ()
{
  foo (std::auto_ptr<A> (new A));
}
------------------------------------------------

Environment:
System: Linux karma 2.6.6 #15 Thu May 13 15:07:54 EDT 2004 i686 i686 i386 GNU/Linux
Architecture: i686

	<machine, os, target, libraries (multiple lines)>
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /home/sss/gcc/gcc/configure --prefix=/usr/local/gcc --enable-threads=posix --enable-long-long --enable-languages=c,c++,f95

How-To-Repeat:

Compile the following with -O2 and run:

y1.cc ------------------------------------------
extern "C" int printf(const char*, ...);

struct auto_ptr
{
  int _M_ptr;
  auto_ptr(int p) : _M_ptr(1) { printf ("a %x\n", this); }
  ~auto_ptr() { printf ("dtor %x %x\n", _M_ptr, this); }
  void release() {  _M_ptr = 0;  }
  auto_ptr(const auto_ptr&a) : _M_ptr (a._M_ptr) { printf ("b %x\n", this); }
};


void foo (auto_ptr a)
{
  a.release();
  printf ("d %x %x\n", a._M_ptr, 0);
}

int main ()
{
  foo (0);
}
------------------------------------------------
------- Additional Comments From snyder at fnal dot gov  2004-06-21 16:43 -------
Fix:
	<how to correct or work around the problem, if known (multiple lines)>

-- 
           Summary: 3.5: problem with argument passing via temporary (breaks
                    auto_ptr)
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: snyder at fnal dot gov
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16115


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