This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/38916] New: auto_ptr_ref conversion incorrectly releases ownership
- From: "janis at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 19 Jan 2009 21:24:23 -0000
- Subject: [Bug libstdc++/38916] New: auto_ptr_ref conversion incorrectly releases ownership
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The ISO C++ standard in section 20.4.5.3 defines the effects and postconditions
of auto_ptr conversions. It says that
template<class Y> operator auto_ptr_ref<Y>() throw();
returns "an auto_ptr_ref<Y> that holds *this". Unlike the other conversions in
the same section, there is no call to release(), so after the call the original
auto_ptr should still own the memory.
The following testcase is based on one from a testing group within IBM (not a
customer-reported problem). It shows that creating a reference to an auto_ptr
releases the memory from that auto_ptr when it should not. I see this behavior
on powerpc*-linux for GCC 3.3 and later. I haven't tried earlier versions.
------------------------------------------------------------------------
// Test the effects and postconditions of auto_ptr conversions, defined
// in the ISO C++ standard in section 20.4.5.3.
#include <memory>
extern "C" void abort (void);
int failures = 0;
#ifdef DBG
#include <iostream>
#define FAILURE(m) \
{ \
std::cout << "line " << __LINE__ << ": " << m << std::endl; \
failures++; \
}
#else
#define FAILURE(m) { failures++; }
#endif
struct X { };
int
main ()
{
X* xptr = new X;
std::auto_ptr<X> ap1 (xptr);
if (ap1.get() != xptr)
FAILURE ("ap1 does not own the memory")
// Construct a reference to ap1; there should be no release.
std::auto_ptr_ref<X> ref (ap1.operator std::auto_ptr_ref<X> ());
if (ap1.get() != xptr)
FAILURE ("ap1 no longer owns the memory after constructing a reference")
// Construct ap2 from the reference. ap2 should now own the memory
// and ap1 should no longer hold it.
std::auto_ptr<X> ap2(ref);
if (ap2.get() != xptr)
FAILURE ("ap2 does not own the memory after constructing from reference")
if (ap1.get() != (X*)0)
FAILURE ("ap1 is not zeroed after constructing ap2 from reference")
if (failures != 0)
abort ();
return 0;
}
------------------------------------------------------------------------
elm3b145% /opt/gcc-nightly/trunk/bin/g++ -DDBG bug.cc
elm3b145% LD_LIBRARY_PATH=/opt/gcc-nightly/trunk/lib ./a.out
line 34: ap1 no longer owns the memory after constructing a reference
Aborted
--
Summary: auto_ptr_ref conversion incorrectly releases ownership
Product: gcc
Version: 4.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: janis at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38916