What happens to the memory allocated by `new` if the constructor throws?
The memory will be automatically freed before the exception propagates.
This is essential, because a) the program never receives a pointer to free, and b) even if it did, it would have no portable way to actually free it since the memory never became an object that you can delete.
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.
Exception throw from constructor and what happen to instance and memory allocation?
Throwing exceptions in the constructor is not a problem to garbage collector, and it definitely does not result in memory leaks.
Although the memory allocated from the heap never makes it to your program, it is available to the internal implementation of the operator new
, which takes care of ensuring that the newly created instance becomes eligible for garbage collection.
Memory Allocation Exception in Constructor
The memory to which a
and b
point would not be automatically freed. Every new[]
must be balanced explicitly with a delete[]
.
Even if your destructor performed the deletion (assuming a
, b
, and c
are class members), then you'd still leak memory. That's because the destructor wouldn't be called in this case since the object failed to construct.
Using std::vector
s would obviate these problems.
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.
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.
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
Specialization of Member Function Template After Instantiation Error, and Order of Member Functions
What Is Namespace Used For, in C++
Clion C++ Can't Read/Open .Txt File in Project Directory
Why There Is No Std::Copy_If Algorithm
Trying to Pass String Literals as Template Arguments
Are Non Dereferenced Iterators Past the "One Past-The-End" Iterator of an Array Undefined Behavior
How Does Sizeof Know the Size of the Operand Array
Sub-Millisecond Precision Timing in C or C++
Calling Delete on Null Pointers - C++03 VS C++11
How to Write Make_Unique() in VS2012
Why Can't Std::Function Bind to C-Style Variadic Functions
How to Convert a String Literal to Unsigned Char Array in Visual C++