public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/38916]  New: auto_ptr_ref conversion incorrectly releases ownership
@ 2009-01-19 21:24 janis at gcc dot gnu dot org
  2009-01-19 22:35 ` [Bug libstdc++/38916] " paolo dot carlini at oracle dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: janis at gcc dot gnu dot org @ 2009-01-19 21:24 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-02-05 12:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-19 21:24 [Bug libstdc++/38916] New: auto_ptr_ref conversion incorrectly releases ownership janis at gcc dot gnu dot org
2009-01-19 22:35 ` [Bug libstdc++/38916] " paolo dot carlini at oracle dot com
2009-01-19 22:45 ` janis at gcc dot gnu dot org
2009-01-19 23:12 ` paolo dot carlini at oracle dot com
2009-01-26 18:42 ` janis at gcc dot gnu dot org
2009-01-26 19:08 ` paolo dot carlini at oracle dot com
2009-02-17 19:04 ` janis at gcc dot gnu dot org
2010-02-05 12:57 ` paolo dot carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).