Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

possiblity to assign any arbitrary pointer to a specialized auto_ptr in vs2005 (Bug?)

5 views
Skip to first unread message

m...@nogga.de

unread,
Feb 10, 2006, 9:57:08 AM2/10/06
to
Hello,

is it only me, or is it really possible to assign any arbitrary pointer
to a specialized auto_ptr? E.g. the following code will compile in
vs2005 with any problems:

// assigning a pointer in the constructor
std::auto_ptr<CDerived> test1 (new int);
std::auto_ptr<CDerived> test2 (new double);

// assigning a pointer in the copy constructor
std::auto_ptr<CDerived> test3 = new int;
std::auto_ptr<CDerived> test4 = new double;


The reason for this behavior is the implicit conversion via the
auto_ptr_ref class, which casts away the type information into a void *
and from there back into the concrete type in the auto_ptr classes again.

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
};


With this definition it is e.g possible to write:

auto_ptr_ref <double> d1 = new int;
auto_ptr_ref <double> d2 = new char;
d1 = d2;

while it is not legal to write

auto_ptr_ref <int> i1 = new int;
d1 = i1;


This conversion via the void pointer allows that you can stuff any
pointer into a concrete auto_ptr class.

I came across this problem in the following code:

auto_ptr <CBase> MakeClass ()
{
return new CDerived ();
}

void main ()
{
auto_ptr <CBase> test (MakeClass ());
}


This was legal code in VC6 and will result in a compile error in VC7.1.
In VS2005 this code will compile again, but will result in a runtime
error, since the implicit conversion via the auto_ptr_ref will result in
a double dereferencing in the constructor of auto_ptr again.

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
}

I know, the function should have been written to return a
auto_ptr<CBase> in the first place. But I expect this is a common
programming error, even if the compiler will not through any warning or
error about this.


Do I get something completely wrong here, or is this a bug in vs2005.

Best regards
Dirk

Ulrich Eckhardt

unread,
Feb 13, 2006, 3:19:35 AM2/13/06
to
m...@nogga.de wrote:
> is it only me, or is it really possible to assign any arbitrary pointer
> to a specialized auto_ptr? E.g. the following code will compile in
> vs2005 with any problems:
>
> // assigning a pointer in the constructor
> std::auto_ptr<CDerived> test1 (new int);
> std::auto_ptr<CDerived> test2 (new double);
>
> // assigning a pointer in the copy constructor
> std::auto_ptr<CDerived> test3 = new int;
> std::auto_ptr<CDerived> test4 = new double;

Just for your info, both of the above should not compile, the first couple
because of a type mismatch the second one also because auto_ptr has an
explicit ctor.

> The reason for this behavior is the implicit conversion via the
> auto_ptr_ref class, which casts away the type information into a void *
> and from there back into the concrete type in the auto_ptr classes again.
>
> 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
> };

I can't fathom the reasons for that, but two things strike me here:
- the ctor is not explicit
- use of a void pointer even though the exact type is known
I'd further check if this still works for cv qualified auto_ptrs, though I'm
not sure it's supposed to...

> I came across this problem in the following code:
>
> auto_ptr <CBase> MakeClass ()
> {
> return new CDerived ();
> }

This is ill-formed, too, because auto_ptr's ctor is explicit (it should
be...) so you have to explicitly create the auto_ptr.

Uli

m...@nogga.de

unread,
Feb 13, 2006, 9:46:16 AM2/13/06
to
Ulrich Eckhardt schrieb:

> m...@nogga.de wrote:
>> is it only me, or is it really possible to assign any arbitrary pointer
>> to a specialized auto_ptr? E.g. the following code will compile in
>> vs2005 with any problems:
>>
>> // assigning a pointer in the constructor
>> std::auto_ptr<CDerived> test1 (new int);
>> std::auto_ptr<CDerived> test2 (new double);
>>
>> // assigning a pointer in the copy constructor
>> std::auto_ptr<CDerived> test3 = new int;
>> std::auto_ptr<CDerived> test4 = new double;
>
> Just for your info, both of the above should not compile, the first couple
> because of a type mismatch the second one also because auto_ptr has an
> explicit ctor.

That is exactly my point. It does compile on VS2005.

>> I came across this problem in the following code:
>>
>> auto_ptr <CBase> MakeClass ()
>> {
>> return new CDerived ();
>> }
>
> This is ill-formed, too, because auto_ptr's ctor is explicit (it should
> be...) so you have to explicitly create the auto_ptr.

Yes, that is true. But I would say it is common programming mistake and
exactly the reason that it does compile is strange. And further it will
produce a runtime error.


As I mentioned, the problem is, that the code compiled and did run ok on
VS6, it was a compile error on VS7 and VS7.1. In VS8 it compiles again
but produces a runtime error.

Best regards
Dirk

Carl Daniel [VC++ MVP]

unread,
Feb 13, 2006, 10:26:54 AM2/13/06
to
m...@nogga.de wrote:
> As I mentioned, the problem is, that the code compiled and did run ok
> on VS6, it was a compile error on VS7 and VS7.1. In VS8 it compiles
> again but produces a runtime error.

I'd suggest that you open a bug report:

http://lab.msdn.microsoft.com/productfeedback

If you do so, please post a link to the bug report here so that others
following this thread can add their votes to it.

-cd


m...@nogga.de

unread,
Feb 13, 2006, 12:00:32 PM2/13/06
to
Hello,

thanks, I didn't knew where to find the public accessible bug tracking
system. I have found the issue in the system under the BugID FDBK44450

Simply search for auto_ptr.

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=cbbcede6-6399-42fe-b555-5c69b7b70821

The bug was opened on the 22.01.2006. So around the same day when I
first encountered this issue also ;-)

Thanks for the info
Dirk


Carl Daniel [VC++ MVP] schrieb:

0 new messages