1

When a bad_alloc exception is thrown in a constructor, in which multiple objects are created, what must be done to clean up the memory. Ex.

class Object
{
   private:
     A * a;
     B  * b;

 public:

   Object()
   {
       a= new A();
       b= new B(); //So if a bad_alloc is called here, how is a deleted???
   }
} 

My intuition is to place each call to new in a separate try catch bloc, and delete all objects for which new was called previously but this is too verbose (the first try bloc calls no destructor, the second class that of the first, the 3rd calls that of the first two etc). My question is: What is the most common way to handle this?

Also, lets say the class objects contains a object not created with new (because it is on the stack), is its destructor called automatically?

11
  • 4
    Normally, you use std::unique_ptr.
    – avakar
    Sep 18, 2014 at 16:52
  • 1
    This looks like C# or Java code, not C++ code. Sep 18, 2014 at 17:04
  • Implement the class to effect the "Rule of Zero". Here std::unique_ptr is an option.
    – Niall
    Sep 19, 2014 at 11:28
  • If bad_alloc is thrown when you allocate a, you can put it inside its own try block. try {a = new A();} catch (...) {delete a;} try {b = new B();} catch (...) {delete a; delete b;}; could solve your problem.
    – rsethc
    Sep 19, 2014 at 11:50
  • 1
    @rsethc, no, no, one thousand times no. That code is ugly, and not even correct. Why are you calling delete a when a never got initialized? That will try to delete a garbage pointer. Same problem for delete b. Also, because you swallow the exceptions when the constructor exits both pointers will be left containing garbage values. Go to jail, go directly to jail, do not pass Go, do not collect £200. (But thank you for proving why manual exception-handling should be avoided like the plague.) Sep 19, 2014 at 11:59

2 Answers 2

6

You want to use smart pointers:

class Object {
    std::unique_ptr<A> a;
    std::unique_ptr<B> b;

public:
    Object() : a(make_unique<A>()), b(make_unique<B>()) {}
}
3
  • 1
    This works because of the explanation here: See: stackoverflow.com/questions/9122331/… Sep 18, 2014 at 16:58
  • Why the calls to release()?
    – interjay
    Sep 18, 2014 at 16:59
  • Agreed with interjay, I also don't think .release() should be there. make_unique<A>() returns a unique_ptr<A> rvalue, so can be move-assigned to a.
    – user743382
    Sep 18, 2014 at 17:01
2

Firstly I fixed your code because it is a C++ question, so it has to be written as C++. A constructor might fail with exceptions other than bad_alloc.

Your options are there:

  • Do not store pointers but store the objects. These are constructed automatically (or via the initialiser list) and yes will automatically be cleaned up if created. This can be better but means they need to be fully defined, i.e. their headers included, and you may be trying to hide your implementation detail / decouple..

  • Store some kind of smart pointer like unique_ptr which is really an object so gets cleaned up like an object, deleting the underlying pointer if there is one.

  • Store a regular pointer in your class but use unique_ptr (or auto_ptr if unique_ptr is not available) during the constructor and, at the end when you know everything has constructed properly, you can release your smart pointers into your member variables.

The latter solution would look like:

 // header file
 //
class A; // implementation hidden on purpose
class B; // ditto

class Object
{
   private:
      A * a;
      B * b;

   public:
     Object();
     ~Object();

    private:

 // if not using C++11 do not put in =delete but still declare these

    Object( const Object & ) = delete;
    Object& operator=( const Object & ) = delete;

};

// cpp file

#include "object.h"
#include "a.h"
#include "b.h"

Object::Object()
   : a( nullptr ), // use NULL or 0 if nullptr not available
     b( nullptr )
{
    std::unique_ptr< A > pa( new A ); // might throw
    std::unique_ptr< B > pb( new B ); // might throw (1)

    a = pa.release(); // cannot throw
    b = pb.release(); 
}

Object::~Object()
{
     delete b;
     delete a;
}

(1) if it throws pa which is local will have its destructor invoked which will delete the pointer you created with new.

Note: if you don't have unique_ptr available, auto_ptr will serve just as well here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.