What happens to heap allocated memory when exceptions occur during the constructing 'new' objects?

c++, memory

Solution

It just works. The memory will be released before your exception handling block is executed.

Problem

What happens to the allocated memory when a class throws an exception during construction and how would you handle something like this. For example: ``` std::auto_ptr<ThirdPartyClass> au_tpc; try { au_tpc.reset(new ThirdPartyClass()); } catch(...) { // What happened to the memory allocated of // sizeof(ThirdPartyClass) for the new instance? } ```

Original source