Article View: comp.std.c++
Article #2428Re: auto_ptr<T> as function return type
From: Eric Biederman
Date: Sun, 05 Nov 1995 09:15
Date: Sun, 05 Nov 1995 09:15
61 lines
3579 bytes
3579 bytes
In article <47c7pe$722@eclipse.eng.sc.rolm.com> eddy@clipper.robadome.com
(eddy Gorsuch) writes:
In article <47b1um$fvs@infoserv.rug.ac.be>,
Stein Somers <uunet!PROBLEM_WITH_INEWS_GATEWAY_FILE!somers@ncar.UCAR.EDU>
wrote:
[...]
>This has been mentioned earlier, of course. So I'd like to repost
>this recent uncountered reply:
>
>Eric Biederman (ebiederm@cse.unl.edu) wrote:
>: The problem is that you are passing it as a standard pointer type.
>: What happens if you were to run out of stack after the call to release
>: and before the call to the constructor, or reset. And an exception is
>: thrown.
[ hiden cut ]
Ummm, who throws an exception if you run out of stack? I thought that came
in as a signal (or other hardware error), not as a C++ exception.
[note: the useful way would for the signal to throw an exception. It
is an error appropriate to an exception. ]
[note: who throws exceptions, _now_? ]
Sorry, that was a bad example so acknowledged in a part you cut out.
A more realistic example happens if a signal handler is called
asynchronously. It just happens that that happened while the call to
the autoptr's constructor or member reset was being formed upon the
stack. This ill timed call to the signal code then throws an
exception. Oops memory leak!
On the other hand if the memory was continually in an autoptr it would
be _safe_ from this (assuming the internal code for autoptr assignment
was written carefully, to avoid these kinds of problems).
By the same token, what happens if you allocate some storage, then run
out of stack space while trying to call the auto_ptr's constructor (or
reset())? This leaves the same memory leak. I don't think these cases are
(nor should they be) covered by the C++ standard. They are too machine/OS
dependant.
[ ... ]
But that's transfering a pointer into an autoptr, the pointer makes no
guarantees of safety in that case. But the __autoptr__ does!
Returning the autoptr to a normal pointer violates all gaurantes of
safety. Assuming that you will still have safety is bad. It's as bad
as assuming all objects can be aligned on any address, or the bit
pattern of static_cast<void *>(0) is 0, or etc.
Once your pointer is in autoptr it's _safe_. While your pointer is in
autoptr it should remain _safe_.
The issue of running out of stack space is not what I was arguing
about. Simply my thought of a synchronous example that could cause
problems. A very unrealistic example.
Eric
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<EBIEDERM.95Nov4234739@cse.unl.edu>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<47616o$lvr@bcarh8ab.bnr.ca> <VANDEVOD.95Oct31222003@juicer.cs.rpi.edu> <9530600.21749@mulga.cs.mu.OZ.AU> <47b1um$fvs@infoserv.rug.ac.be> <47c7pe$722@eclipse.eng.sc.rolm.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 05 Nov 95 09:15:41 GMT Approved: fjh@cs.mu.oz.au From: ebiederm@cse.unl.edu (Eric Biederman) Newsgroups: comp.std.c++ Subject: Re: auto_ptr<T> as function return type X-Original-Date: 05 Nov 1995 05:47:39 GMT Organization: University of Nebraska--Lincoln Message-ID: <EBIEDERM.95Nov4234739@cse.unl.edu> References: <47616o$lvr@bcarh8ab.bnr.ca> <VANDEVOD.95Oct31222003@juicer.cs.rpi.edu> <9530600.21749@mulga.cs.mu.OZ.AU> <47b1um$fvs@infoserv.rug.ac.be> <47c7pe$722@eclipse.eng.sc.rolm.com> In-Reply-To: eddy@clipper.robadome.com's message of 03 Nov 95 18:11:39 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJyBEOEDnX0m9pzZAQHVfQGAi2hoiJsPwZOzD4XDydrVYfRduwsZhljU 3OQYh99UCwC4j1TUvpoxUY2nGj57wiDM =u3ff