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?