Who Deletes the Memory Allocated During a "New" Operation Which Has Exception in Constructor

Who deletes the memory allocated during a new operation which has exception in constructor?

You should refer to the similar questions here and here.
Basically if the constructor throws an exception you're safe that the memory of the object itself is freed again. Although, if other memory has been claimed during the constructor, you're on your own to have it freed before leaving the constructor with the exception.

For your question WHO deletes the memory the answer is the code behind the new-operator (which is generated by the compiler). If it recognizes an exception leaving the constructor it has to call all the destructors of the classes members (as those have already been constructed successfully prior calling the constructor code) and free their memory (could be done recursively together with destructor-calling, most probably by calling a proper delete on them) as well as free the memory allocated for this class itself. Then it has to rethrow the catched exception from the constructor to the caller of new.
Of course there may be more work which has to be done but I cannot pull out all the details from my head because they are up to each compiler's implementation.

Constructor Exception and Dynamic Allocation

No. There's nothing to delete, because the object never got constructed. The compiler will take care of freeing the memory that was allocated.

How does C++ free the memory when a constructor throws an exception and a custom 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.

Is memory allocated for an object automatically deleted if an exception is thrown in a constructor?

Yes, the memory allocated for the CFoo object will be freed in this case.

Because the exception due to the failed allocation causes the CFoo constructor to fail to complete successfully the new-expression is guaranteed to free the memory allocated for that CFoo object.

This guarantee is specified in 5.3.4 [expr.new] / 17 of ISO/IEC 14882:2003.

Note, that it is always advisable to assign the result of a dynamic allocation to a smart pointer to ensure proper clean up. For example, if there was further code in CFoo constructor and that threw an exception the CBar object already successfully allocated earlier in the constructor would be leaked.

how to delete the memory of data member when exception thrown by constructor

Use RAII and you don't have to deal with it:

class ConWithException
{
public:
ConWithException() : _pBuf(new int[100])
{
cout << "before throw exception in constructor" << endl;
throw std::runtime_error("Exception in Constructor!");
}

private:
std::unique_ptr<int[]> _pBuf;
};

Does the memory get released when I throw an exception?

A call to

new B();

resolves in two things:

  • allocating with an operator new() (either the global one or a class specific one, potentially a placement one with the syntax new (xxx) B())
  • calling the constructor.

If the constructor throw, the corresponding operator delete is called. The case where the corresponding delete is a placement one is the only case where a placement delete operator is called without the syntax ::operator delete(). delete x; or delete[] x; don't call the placement delete operators and there is no similar syntax to placement new to call them.

Note that while the destructor of B will not be called, already constructed subobjects (members or B and base classes of B) will be destructed before the call to operator delete. The constructor which isn't called is the one for B.

What happens to heap allocated memory when exceptions occur during the constructing 'new' objects?

It just works. The memory will be released before your exception handling block is executed.



Related Topics



Leave a reply



Submit