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.
The first line:
should result in a compiler error. Because there is no implicit conversion from the raw
AClasspointer to anauto_ptr(the constructor for anauto_ptrthat takes a raw pointer is markedexplicit), initialization using the ‘copy initializer’ form is not permitted.VC9 gives the following error:
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&wa=wsignin1.0