[Interest] MSVC 2013 Bug?

Thiago Macieira thiago.macieira at intel.com
Thu Nov 12 22:48:08 CET 2015


On Friday 13 November 2015 00:28:46 Igor Mironchik wrote:
> On 13.11.2015 00:15, Matthew Woehlke wrote:
> > On 2015-11-12 15:56, Igor Mironchik wrote:
> >> Can you, please, explain why allocating memory when throwing exception
> >> is bad practice? Or just can you give a link on any article about this
> >> question.... Thank you.
> > 
> > What if the exception occurred because you are out of memory?
> 
> Then will be std::bad_alloc instead of my custom exception. Am I right?
> 
> Then if I will catch std::bad_alloc somewhere and correctly will do
> cleanup and stop, for example, then there will not be problems. Right?

Not necessarily. It's possible that the OOM situation got reported as a 
different error, like failure to open a file (fopen will return NULL on all 
error conditions).

And even if you clean up, there's no guarantee that you now have enough memory 
to allocate to throw your exception. In fact, there's no guarantee at any 
point that you'll have enough memory during exception throwing. If you get a 
std::bad_alloc during throw, your application terminates instead of reporting 
the error.

So don't allocate memory in response to failures.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list