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 { char *heap = new char [50]; //let exception occur here delete[] heap; } catch (...) { cout << "Error, leaving function now"; //delete[] heap; doesn't work of course, heap is unknown to compiler return 1; }
How can I free memory after the heap was allocated and exception occurred before calling delete[] heap
? Is there a rule not to allocate memory on heap in these try .. catch blocks?
To avoid writing multiple try catch async await in a function, a better option is to create a function to wrap each try catch. The first result of the promise returns an array where the first element is the data and the second element is an error. And if there's an error, then the data is null and the error is defined.
So using few try-catch blocks shouldn't affect a performance at all. In some opinion writing code that way obfuscates the code and does not even recall "clean code", in others opinion it's better to use try only for lines which can actually throw any exception. It's up to you decide (or the team convention).
try catch block does not slow down your program at all and is basically a standard for catching exceptions. Try Catch statements is basically your safe net when it comes to bugs in your code/program.
Study the RAII idiom (Resource Acquisition Is Initialization)! See e.g. the Wikipedia article on RAII.
RAII is just the general idea. It is employed e.g. in the C++ standard library's std::unique_ptr
or std::shared_ptr
template classes.
Very brief explanation of the RAII idiom:
Basically, it is the C++ version of try..finally
blocks found in some other languages. The RAII idiom is arguably more flexible.
It works like this:
You write a wrapper class around your resource (e.g. memory). The destructor is responsible for freeing the resource.
You create, as a local (automatic) variable, an instance of your wrapper class in a scope. Once program execution leaves that scope, the object's destructor will be called, thereby releasing the resource (e.g. memory).
The important point is that it doesn't matter how the scope is exited. Even if an exception is thrown, the scope is still exited and the wrapper object's destructor is still called.
Very crude example:
// BEWARE: this is NOT a good implementation at all, but is supposed to // give you a general idea of how RAII is supposed to work: template <typename T> class wrapper_around { public: wrapper_around(T value) : _value(value) { } T operator *() { return _value; } virtual ~wrapper_around() { delete _value; // <-- NOTE: this is incorrect in this particular case; // if T is an array type, delete[] ought to be used } private: T _value; }; // ... { wrapper_around<char*> heap( new char[50] ); // ... do something ... // no matter how the { } scope in which heap is defined is exited, // if heap has a destructor, it will get called when the scope is left. // Therefore, delegate the responsibility of managing your allocated // memory to the `wrapper_around` template class. // there are already existing implementations that are much better // than the above, e.g. `std::unique_ptr` and `std::shared_ptr`! }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With