auto_ptr's exception safe

Discussion in 'C++' started by Kuba_O, Jan 25, 2006.

  1. Kuba_O

    Kuba_O Guest

    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 :D

    Regards
    Kuba
    --

    empty
     
    Kuba_O, Jan 25, 2006
    #1
    1. Advertisements

  2. Kuba_O

    Guest

    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
     
    , Jan 25, 2006
    #2
    1. Advertisements

  3. Kuba_O

    Kuba_O Guest

    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 :D
    > Of course, auto_ptr has a copy ctor and an
    > operator= defined which avoid the double-deletion bug you have.

    Yeap.

    out.
    --

    empty
     
    Kuba_O, Jan 25, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,797
    Dave Rahardja
    Jan 11, 2005
  2. Replies:
    3
    Views:
    1,408
  3. George2

    auto_ptr in exception safety design

    George2, Mar 29, 2008, in forum: C Programming
    Replies:
    0
    Views:
    475
    George2
    Mar 29, 2008
  4. Replies:
    8
    Views:
    437
    James Kanze
    Apr 30, 2008
  5. Gabriel Rossetti
    Replies:
    0
    Views:
    1,857
    Gabriel Rossetti
    Aug 29, 2008
  6. Sousuke
    Replies:
    9
    Views:
    1,463
    Bart van Ingen Schenau
    Mar 16, 2010
  7. siddhu
    Replies:
    5
    Views:
    1,182
    Michael Doubez
    May 18, 2011
  8. Replies:
    1
    Views:
    559
    Brian Candler
    Aug 12, 2003
Loading...