tags:

views:

696

answers:

4

Seem to be having an issue with std::auto_ptr and assignment, such that the object referenced seems to get trashed for some reason.

std::auto_ptr<AClass> someVar = new AClass();  // should work, but mangles content
std::auto_ptr<AClass> someVar( new AClass() ); // works fine.
std::auto_ptr<AClass> someVar = std::auto_ptr<AClass>(new AClass()); // works fine.

std::auto_ptr<AClass> someVar;
someVar.reset( new AClass() ); // works fine.

I've traced it through, and it appears (via watching the values in the debugger) that the problem occurs in the transfer of the pointer from the temporary std::auto_ptr_byref() that is created to wrap the rhs pointer. That is the value contained in _Right on entering the auto_ptr(auto_ptr_ref<_Ty> _Right) function is correct, but the value in _Myptr on leaving is junk.

template<class _Ty>
    struct auto_ptr_ref
     { // proxy reference for auto_ptr copying
    auto_ptr_ref(void *_Right)
     : _Ref(_Right)
     { // construct from generic pointer to auto_ptr ptr
     }

    void *_Ref; // generic pointer to auto_ptr ptr
    };

template<class _Ty>
class auto_ptr
    { // wrap an object pointer to ensure destruction
public:
typedef _Ty element_type;

explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()
    : _Myptr(_Ptr)
    { // construct from object pointer
    }

auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()
    : _Myptr(_Right.release())
    { // construct by assuming pointer from _Right auto_ptr
    }

auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
    { // construct by assuming pointer from _Right auto_ptr_ref
    _Ty **_Pptr = (_Ty **)_Right._Ref;
    _Ty *_Ptr = *_Pptr;
    *_Pptr = 0; // release old
    _Myptr = _Ptr; // reset this
    }
auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // assign compatible _Right._Ref (assume pointer)
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0; // release old
reset(_Ptr); // set new
return (*this);
}

At first I thought it was messing up the inheritance and slicing off interfaces, however this happens even if the class only has one parent class.

We can avoid doing = new if we remember, either by using brackets or changing to have a explicit std::auto_ptr temp on the rhs, this is of course error prone.

Is it just this version of the library being broken, or some underlying thing I'm just not getting?

We also noticed a similar issue with assigning a std::auto_ptr to a boot::shared_ptr though we removed that entirely now and I don't recall which syntax caused the issue.

A: 

Edited: Michael is quite right, it's a compile error, so which compiler are you using? You need to call reset to put a new value into the auto_ptr.

Jesse Pepper
Using MSVC 2005 sp1
Greg Domjan
+8  A: 

The first line:

std::auto_ptr<AClass> someVar = new AClass();  // should work, but mangles content

should result in a compiler error. Because there is no implicit conversion from the raw AClass pointer to an auto_ptr (the constructor for an auto_ptr that takes a raw pointer is marked explicit), initialization using the 'copy initializer' form is not permitted.

VC9 gives the following error:

C:\temp\test.cpp(23) : error C2440: 'initializing' : cannot convert from 'AClass *' to 'std::auto_ptr<_Ty>'

Other compilers I've tried (GCC 3.4.5, Comeau C/C++ 4.3.10.1, Digital Mars) give a similar error.

EDIT:

It looks like this is in fact a bug in VS2005's implementation of auto_ptr<> (not sure if it was introduced in SP1 or was in VS2005 from the start) that got fixed in VS2008. Here's the MS Connect bug record for the problem:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842&amp;wa=wsignin1.0

Michael Burr
The first line has nothing to do with operator=(); this is an initialization, not an assignment. there's a ctor that takes an auto_ptr and a ctor that takes a raw pointer. the raw pointer ctor is explicit, and so there's no conversion from raw pointer to auto_ptr.
wilhelmtell
@wilhelmtell - you are correct. I fixed that misstatement about the same time as you pointed it out.
Michael Burr
While one optimisation would be to call the ctor and initialize, it doesn't do that. In MSVC 2005 auto_ptr_ref was introduced in the library and instead that is constucted as a temporary rhs.In previous versions I could not do line 1, and yes I used to get that error.
Greg Domjan
As now shown in copy from the <memory> file provided in the library of MSVC8, you are correct that raw ptr is explicit, however the auto_ptr_ref is not explicit, and neither is the constructor to auto_ptr_ref. :( the change in library is causing the biggest pain!
Greg Domjan
Your link is slightly mangled. (I don't have enough rep to fix it.)
zdan
Corrected the link. ^_^ ...
paercebal
+2  A: 

See http://stackoverflow.com/questions/372665/c-instance-initialization-syntax for some discussion of the difference between your first and second lines.

Kristopher Johnson
This was interesting, but did not directly help resolve the issue
Greg Domjan
+3  A: 

Known bug in VC++ between VC6 and VC9: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842

MSalters