I see the following constructs:
new X
will free the memory if X
constructor throws.
operator new()
can be
Fundamentally, if there is no delete
operator which corresponds to the new
operator, then nothing is done. Nothing is done also in the case of placement new, because the corresponding placement delete operator is a no-op. The exception is not diverted: it continues its course, so the caller of the new has the opportunity (and responsibility) for freeing the memory allocated.
Placement new is called that because it is used to place the object in memory otherwise acquired; since the memory was not acquired by the new operator, it would be unlikely that it could be released by the delete operator. In practice, the question is moot because (since C++03, at least) it is not permitted to replace the placement new operator (which has prototype operator new(size_t, void*)
or delete (operator delete(void*, void*)
). The supplied placement new operator returns its second argument, and the supplied placement delete operator is a no-op.
Other new
and delete
operators may be replaced, either globally or for a specific class. If a custom new
operator is called, and the constructor throws an exception, and there is a corresponding delete
operator, then that delete operator will be called to clean up before the exception is propagated. However, it is not an error if there is no corresponding delete
operator.