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 the Constructor throws the memory allocated for the object is auto-magically returned to the system.

Note the destructor of the class that threw will not be called.  

But the destructor of any base class (where the base constructor has completed) will also be called.

Note:  

As most other people have noted members may need some clean up.

Members that have been fully initialized will have their destructors called, but if you have any RAW pointer members that you own (ie delete in the destructor) you will have to do some clean up before you do the throw (another reason not to use owned RAW pointers in your class). 

```
#include <iostream>

class Base
{
    public:
        Base()  {std::cout << "Create  Base\n";}
        ~Base() {std::cout << "Destroy Base\n";}
};

class Deriv: public Base
{
    public:
        Deriv(int x)    {std::cout << "Create  Deriv\n";if (x > 0) throw int(x);}
        ~Deriv()        {std::cout << "Destroy Deriv\n";}
};

int main()
{
    try
    {
        {
            Deriv       d0(0);  // All constructors/Destructors called.
        }
        {
            Deriv       d1(1);  // Base constructor and destructor called.
                                // Derived constructor called (not destructor)
        }
    }
    catch(...)
    {
        throw;
        // Also note here.
        // If an exception escapes main it is implementation defined
        // whether the stack is unwound. By catching in main() you force
        // the stack to unwind to this point. If you can't handle re-throw
        // so the system exception handling can provide the appropriate
        // error handling (such as user messages).
    }
}

```
I think it's kind of wierd for a constructor to raise an exception.
Could you have a return value and test it in your main?

```
class Blah
{
   public:

   Blah()
       {
           if Error
           {
              this.Error = "oops";
           }
        }
};

void main()
{
Blah* b = NULL;

b = new Blah();

if (b.Error == "oops")
{
   delete (b);
   b = NULL;
}

```