Question: I really can't believe I couldn't find a clear answer to this...

How do you free the memory allocated after a C++ class constructor throws an exception, in the case where it's initialised using the `new` operator. E.g.:

```
class Blah
{
public:
  Blah()
  {
    throw "oops";
  }
};

void main()
{
  Blah* b = NULL;
  try
  {
    b = new Blah();
  }
  catch (...)
  {
    // What now?
  }
}

```

When I tried this out, `b` is NULL in the catch block (which makes sense).

When debugging, I noticed that the conrol enters the memory allocation routine BEFORE it hits the constructor. 

This on the MSDN website [seems to confirm this](http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx):

> 
> When new is used to allocate memory
>  for a C++ class object, the object's
>  constructor is called after the memory
>  is allocated.
> 
> 
> 

So, bearing in mind that the local variable `b` is never assigned (i.e. is NULL in the catch block) how do you delete the allocated memory? 

It would also be nice to get a cross platform answer on this. i.e., what does the C++ spec say?

CLARIFICATION: I'm not talking about the case where the class has allocated memory itself in the c'tor and then throws. I appreciate that in those cases the d'tor won't be called. I'm talking about the memory used to allocate *THE* object (`Blah` in my case).

Answer: 
If an object cannot complete destruction because the constructor throws an exception, the first thing to happen (this happens as part of the constructor's special handling) is that all member variables to have been constructed are destroyed - if an exception is thrown in the initializer list, this means that only elements for which the initializer has completed are destroyed.

Then, if the object was being allocated with `new`, the appropriate deallocation function (`operator delete`) is called with the same additional arguments that were passed to `operator new`. For instance, `new (std::nothrow) SomethingThatThrows()` will allocate memory with `operator new (size_of_ob, nothrow)`, attempt to construct `SomethingThatThrows`, destroy any members that were successfully constructed, then call `operator delete (ptr_to_obj, nothrow)` when an exception is propagated - it won't leak memory.

What you have to be careful is allocating several objects in succession - if one of the later ones throws, the previous ones will not be automatically be deallocated. The best way around this is with smart pointers, because as local objects their destructors will be called during stack unwinding, and their destructors will properly deallocate memory.
The long and short of it is that if you haven't made any allocations of other entities in you object(as in your example) then the memory that was allocated will be deleted automatically. However, any new statements(or anything else that directly manages memory) needs to be handled in a catch statement in the constructor, Otherwise the object is deleted without deleting it's subsequent allocations and you, my friend, have a leak.