Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > auto_ptr's exception safe

Reply
Thread Tools

auto_ptr's exception safe

 
 
Kuba_O
Guest
Posts: n/a
 
      01-25-2006
Hello, i've got simple question about std::auto_ptr: what makes it is
exceptions safe?
Lets say i have class "int_smart_ptr" implemented like this:

class int_smart_ptr
{
private:
int *int_obj;
public:
explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
~int_smart_ptr()throw(){delete int_obj;}
};

Is this class exception safe? When exception occur in scope in
int_smart_ptr was use, would destructor be call?
If yes, why?
If not, what feature of std::auto_ptr decide it is safe in case of
exception?

I hope someone understand my english

Regards
Kuba
--

empty

 
Reply With Quote
 
 
 
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      01-25-2006

Kuba_O wrote:
> Hello, i've got simple question about std::auto_ptr: what makes it is
> exceptions safe?


Simple: a proper copy ctor, assignment and destructor.

> Lets say i have class "int_smart_ptr" implemented like this:
>
> class int_smart_ptr
> {
> private:
> int *int_obj;
> public:
> explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
> ~int_smart_ptr()throw(){delete int_obj;}
> };
>
> Is this class exception safe?


No. If you copy one, and an exception is thrown, both the original and
the copy will be destroyed. Both destructors will attempt to delete the
same int*, leading to a double-deletion bug.

> When exception occur in scope in int_smart_ptr was use, would
> destructor be call? If yes, why?


Yes, because destructors are always called for all objects from the
scope being exited. Of course, auto_ptr has a copy ctor and an
operator= defined which avoid the double-deletion bug you have.

HTH,
Michiel Salters

 
Reply With Quote
 
 
 
 
Kuba_O
Guest
Posts: n/a
 
      01-25-2006
25.01.2006, :
> > Is this class exception safe?

>
> No. If you copy one, and an exception is thrown, both the original and
> the copy will be destroyed. Both destructors will attempt to delete the
> same int*, leading to a double-deletion bug.

I know that, it was just simple sample

> Yes, because destructors are always called for all objects from the
> scope being exited.

I did not know that, now everything is clear.
Thanks for inform me about such essential behavior of exceptions
> Of course, auto_ptr has a copy ctor and an
> operator= defined which avoid the double-deletion bug you have.

Yeap.

out.
--

empty

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is differece between exception safe and exception neutral? siddhu C++ 5 05-18-2011 06:29 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Exception safe & Exception Neutral deep_copy smart pointer Nindi73@yahoo.co.uk C++ 3 11-10-2006 10:55 AM
Safe Mode (?) - It is meant to be normal mode but looks like safe mode English Patient Computer Support 3 10-03-2004 11:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57