There are already several questions about this on StackOverflow but none of them adequately capture the nature of throwing exceptions from constructors and the memory implication of doing such in certain cases.
What I'd like to ask is about the memory safety of throwing an exception during construction, assuming we have several allocation objects in construction.
From what I understand (what we've been taught) is that if an exception is thrown from the constructor, then the destructor doesn't have a guarantee to be called, so the object could be left in an inconsistent state. Not sure if I interpreted that correctly.
So if we have objects such as vectors of deques in a more complex constructor (assuming we aren't using an Init() method so we don't violate RAII), could this mean their memory doesn't get released?.. Or do they get destructed because they're in the scope of the constructor?..
Another point of view is if we have a deque as a private variable of the class, and we add items to it during construction, which then fails. Since this deque was made during initialization, does it mean it won't get destructed / released when an exception is thrown during object construction, leading to a potential leak?
This is all assuming we're not using a separate class to encapsulate our throwing class, to catch exceptions and handle them alone.
What is the safest way to throw an exception during construction, taking all of this in mind? Should we manually call the destructor before throwing, in case we have manual memory allocations and whatnot?