How to free memory in try-catch blocks?

后端 未结 9 2149
时光说笑
时光说笑 2021-01-31 18:33

I have a simple question hopefully - how does one free memory which was allocated in the try block when the exception occurs? Consider the following code:

try
 {         


        
9条回答
  •  遇见更好的自我
    2021-01-31 18:38

    Either move the new before the try, so that the pointer is still in scope, or use a smart pointer like shared_ptr or unique_ptr (in a pinch, auto_ptr, but it has issues) that will clean up for you on exit. Exceptions are a huge reason why smart pointers are important.

提交回复
热议问题