Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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).

like image 952
John Avatar asked Nov 04 '09 16:11

John


People also ask

What is used to deallocate the memory allocated by the constructor?

Assignment (operator=) It will look similar to the copy constructor, but it will check for self assignment and then deallocate the old memory before allocating new memory.

Which operator is used to deallocate the memory allocated using new '?

The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator.

Which operator is used for deleting a memory at runtime?

In C++, you can allocate memory at runtime using the “new” operator. Once you no longer need to use the variable that you have declared dynamically, you can deallocate the memory occupied by the variable using the “delete” operator in C++.

How does a constructor allocate memory?

A constructor does not allocate memory for the class object its this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator.


1 Answers

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.

like image 93
Kosi2801 Avatar answered Sep 25 '22 08:09

Kosi2801