473,498 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto_ptr assignment crash with VC language extensions

I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
....
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception
You can tell me to disable these damned extensions and stop
bothering....
Oww, how I wish.. Of course I can't disable them because many Windows
headers depend on this feature enabled :-(

Is there any practical way to get rid of this? It makes easier to ruin
a program by accident...

What a damned extension heh? :)
thanks
Diego Martins

Nov 20 '06 #1
10 1882

Diego Martins wrote:
I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception
You can tell me to disable these damned extensions and stop
bothering....
Oww, how I wish.. Of course I can't disable them because many Windows
headers depend on this feature enabled :-(

Is there any practical way to get rid of this? It makes easier to ruin
a program by accident...

What a damned extension heh? :)
thanks
Diego Martins
There could be a problem with the copy constructor, I am not sure but
then you can disable language extensions with /Za but I dont think this
is related to that.

Having said that, what if you initialize it directly like this.

auto_ptr<sBasep(creates());

I think the Reset is making sure that the correct pointer is
initialized to the member of the auto_ptr object.

Nov 20 '06 #2
Diego Martins wrote:
I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception
I had the exact same problem a few days ago. If you search the archive
for my handle (rdilipk) you'd find the relevant post. Victor Bazarov
posted a lengthy explanation after some research into Dinkumware
implementation.

Basically this is not a VC8 specific issue. You *cannot* initialize an
auto_ptr object with an ordinary pointer using the assignment syntax as
you do.

std::auto_ptr<Fooptr = new Foo;

will NOT work.

You need to do:

std::auto_ptr<Fooptr(new Foo);

Refer page 40 of C++ Standard Library by Josuttis for an explanation as
to the why.

Nov 20 '06 #3
"Diego Martins" <jo********@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
>I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception
You can tell me to disable these damned extensions and stop
bothering....
Oww, how I wish.. Of course I can't disable them because many Windows
headers depend on this feature enabled :-(

Is there any practical way to get rid of this? It makes easier to ruin
a program by accident...

What a damned extension heh? :)
You can disable this "extension" by making the auto_ptr_ref constructor
explicit, IIRC. (It's more of a lapse in compiler checking than an
intentional attempt to extend auto_ptr in the library.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 20 '06 #4
VJ
Diego Martins wrote:
>
You can tell me to disable these damned extensions and stop
bothering....
format c:

and use anything else but windows
Nov 21 '06 #5

am******@gmail.com wrote:
Diego Martins wrote:
I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception
You can tell me to disable these damned extensions and stop
bothering....
Oww, how I wish.. Of course I can't disable them because many Windows
headers depend on this feature enabled :-(

Is there any practical way to get rid of this? It makes easier to ruin
a program by accident...

What a damned extension heh? :)
thanks
Diego Martins

There could be a problem with the copy constructor, I am not sure but
then you can disable language extensions with /Za but I dont think this
is related to that.
yeah! i run to /Za, but I can't include winnt.h anymore
even winsock2.h no longer works with /Za (making my "portable" sockets
code useless)
Having said that, what if you initialize it directly like this.

auto_ptr<sBasep(creates());
this works nicely of course
I think the Reset is making sure that the correct pointer is
initialized to the member of the auto_ptr object.
reset is the only way to replace the owned pointer. however, it is easy
to do a mistake and use operator= instead (one must admit it is
intuitive, therefore)

I hate VC++ :(

Diego Martins
HP

Nov 22 '06 #6

rd*****@gmail.com wrote:
Diego Martins wrote:
I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception

I had the exact same problem a few days ago. If you search the archive
for my handle (rdilipk) you'd find the relevant post. Victor Bazarov
posted a lengthy explanation after some research into Dinkumware
implementation.

Basically this is not a VC8 specific issue. You *cannot* initialize an
auto_ptr object with an ordinary pointer using the assignment syntax as
you do.

std::auto_ptr<Fooptr = new Foo;

will NOT work.

You need to do:

std::auto_ptr<Fooptr(new Foo);

Refer page 40 of C++ Standard Library by Josuttis for an explanation as
to the why.
the initialization is not the worst problem, as I posted before.
furthermore VC 8 compiles copy-initalization and assignment to raw
pointers if compiler extensions are allowed.

in the real world, if we need to use VC (to build an Windows exe or a
DLL, of course), we need compiler extensions enabled :(

since we can't go away with this, do you know a safe way to redefine
the auto_ptr interface in order to keep my foot intact?

someone in this thread told to make auto_ptr_ref constructor explicit.
how I can "edit" that?

Diego Martins
HP

Nov 22 '06 #7
"Diego Martins" <jo********@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
rd*****@gmail.com wrote:
>Diego Martins wrote:
I just discovered a serious issue when compiling code using auto_ptr<>
and VC 8.0

consider some sort of create() function

blahblah * create() { return new blahblah; }

and a pretty auto_ptr
auto_ptr<base_blahblahp;

int main() {
...
p.reset(create());

// use p as usual and we will be free of leak :)

}

but if we change that line to
p = create();

the WILL compile on VC 8 and may raise an access violation exception

I had the exact same problem a few days ago. If you search the archive
for my handle (rdilipk) you'd find the relevant post. Victor Bazarov
posted a lengthy explanation after some research into Dinkumware
implementation.

Basically this is not a VC8 specific issue. You *cannot* initialize an
auto_ptr object with an ordinary pointer using the assignment syntax as
you do.

std::auto_ptr<Fooptr = new Foo;

will NOT work.

You need to do:

std::auto_ptr<Fooptr(new Foo);

Refer page 40 of C++ Standard Library by Josuttis for an explanation as
to the why.

the initialization is not the worst problem, as I posted before.
furthermore VC 8 compiles copy-initalization and assignment to raw
pointers if compiler extensions are allowed.

in the real world, if we need to use VC (to build an Windows exe or a
DLL, of course), we need compiler extensions enabled :(

since we can't go away with this, do you know a safe way to redefine
the auto_ptr interface in order to keep my foot intact?

someone in this thread told to make auto_ptr_ref constructor explicit.
how I can "edit" that?
See C:\Program Files\Microsoft Visual Studio 8\VC\include\memory.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 23 '06 #8

P.J. Plauger wrote:
"Diego Martins" <jo********@gmail.comwrote in message
someone in this thread told to make auto_ptr_ref constructor explicit.
how I can "edit" that?

See C:\Program Files\Microsoft Visual Studio 8\VC\include\memory.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
I refuse to edit M$ header files

How will it prevent future errors when I deploy the sources to other
sites or Visual C updates?

:(

Nov 27 '06 #9
Diego Martins wrote:
P.J. Plauger wrote:
>"Diego Martins" <jo********@gmail.comwrote in message
>>someone in this thread told to make auto_ptr_ref constructor
explicit. how I can "edit" that?

See C:\Program Files\Microsoft Visual Studio 8\VC\include\memory.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

I refuse to edit M$ header files
They aren't really MS files. If you look at the end, it says Copyright P.J.
Plauger :-)
>
How will it prevent future errors when I deploy the sources to other
sites or Visual C updates?
I would make a copy of that file, edit it, and put it in directory earlier
in the include search list.

And then add this procedure to the installation instruction for my
application.
Bo Persson

Nov 27 '06 #10

Diego Martins skrev:
P.J. Plauger wrote:
"Diego Martins" <jo********@gmail.comwrote in message
someone in this thread told to make auto_ptr_ref constructor explicit.
how I can "edit" that?
See C:\Program Files\Microsoft Visual Studio 8\VC\include\memory.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

I refuse to edit M$ header files

How will it prevent future errors when I deploy the sources to other
sites or Visual C updates?
I agree it is far from an optimal solution, but editing the headerfile
will discover your errors at compile-time. So redeploying your code
should not cause any errors. If your code is to be edited by others,
you could make a note about the patch.

/Peter

Nov 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3455
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and...
10
4460
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
23
1533
by: Jeff | last post by:
After reading all I could find about auto_ptr, I decided to write a little program to test my comprehension: #include <iostream> #include <memory> class A { public: void say_hi() {...
18
2819
by: Dilip | last post by:
I can't believe I am unable to get this simple thing right. I have an auto_ptr class to a structure type. I have it as a member variable in my class. struct Y { SomeType& m_st; Y(SomeType&...
10
2593
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
9
2902
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
864
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
4
2355
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would...
17
10045
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.