Mar 16, 2024 at 7:37pm
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 Mar 16, 2024 at 10:19pm
Mar 16, 2024 at 10:20pm
If the constructor throws the memory will be freed so there is no memory leak. Already constructed data members will have their destructors called.
Mar 16, 2024 at 10:30pm
But the destructor is not called when constructor throws...correct??
Last edited on Mar 16, 2024 at 10:47pm
Mar 16, 2024 at 11:56pm
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.
Mar 17, 2024 at 4:56am
The pitfalls in leaks_resources and leaks_memory exist and are are common outside of constructors too.
Mar 17, 2024 at 7:43pm
Excellent comments! Thank you again!!