if the ctor throws, then the object this has already received the memory (some new or alloc has been executed)... so, does the object this leak?
the only possible way that it didn't leak is if free is called upon ctor throw!!
this happens even if this is an empty class -- no non-static data members!
Correct??
I know the destructor is not called when constructor throws...
Last edited on
If the constructor throws the memory will be freed so there is no memory leak. Already constructed data members will have their destructors called.
But the destructor is not called when constructor throws...correct??
Last edited on
Right. The destructor isn't called, because the object's lifetime doesn't start until its constructor is complete.
That being said, member and base class subobjects get destroyed provided they are already within their lifetime when the exception is thrown.
The pitfalls in leaks_resources and leaks_memory exist and are are common outside of constructors too.
Excellent comments! Thank you again!!