I see the following constructs:
new X
will free the memory if X
constructor throws.
operator new()
can be overloaded.
The canonical definition of an operator new overload is void *operator new(size_t c, heap h)
and the corresponding operator delete
.
The most common operator new overload is placement new, which is void *operator new(void *p) { return p; }
You almost always cannot call delete
on the pointer given to placement new
.
This leads to a single question: How is memory cleaned up when X
constructor throws and an overloaded new
is used?
When a constructor throws an exception the matching delete is called. The destructor is not called for the class that threw but any components of the class that have successfully had their constructors called will have their destructors called.