简体   繁体   中英

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

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?
}

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

Relevant parts of the C++11 Standard: 5.3.4 [expr.new]

8- A new-expression obtains storage for the object by calling an allocation function (3.7.4.1). If the new- expression terminates by throwing an exception, it may release storage by calling a deallocation function (3.7.4.2). If the allocated type is a non-array type, the allocation function's name is operator new and the deallocation function's name is operator delete . If the allocated type is an array type, the allocation function's name is operator new[] and the deallocation function's name is operator delete[] .

18- If any part of the object initialization described above 76 terminates by throwing an exception and a suitable deallocation function can be found, the deallocation function is called to free the memory in which the object was being constructed , after which the exception continues to propagate in the context of the new-expression. If no unambiguous matching deallocation function can be found, propagating the exception does not cause the object's memory to be freed. [ Note: This is appropriate when the called allocation function does not allocate memory; otherwise, it is likely to result in a memory leak. — end note ]

76) This may include evaluating a new-initializer and/or calling a constructor.

In other words, the memory will be freed automatically except under very specific circumstances where the compiler can't find a proper deallocation function (eg. you messed up your custom (de)allocators, or the memory actually doesn't need to be freed).

Check the Item 10 of the Scoot Meyers book 'More Effective C++'.

A important quote from item is:

  • " C++ destroys only fully constructed objects, and an object isn't fully constructed until its constructor has run to completion. ". So, if you throw an exception in the constructor and dont catch it in the the constructor, the destructor will never be called.

For specific solutions, I strongly suggest you to read this item (or better, the entire book).

What happens to the allocated memory when a class throws an exception during construction and how would you handle something like this

This is a good question and correct treatment requires a full-blown article. Good news someone wrote that article already: Constructor Failures .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM