0

I wasn't able to find the answer on SO or I'm not good at search what I need since I'm new to C++. I'm reading C++ primer and it suggests to use shared_ptrs but I want to try to manage memory by myself.

struct A {

 int* x;
 A(): x(new int(5)) {}
 ~A() { delete x; }
 void errorHere() {
   //code...
   throw std::runtime_error("ops");
   //code...
 }

}

Since I've a pointer I must take care of the heap. Now let's say that I run this code:

A test();
try {
 test.errorHere(); //this function throws something, for example std runtime_error
} catch (...) {
  cerr << "error here!";
}

As you can see the errorHere(); function throws an exception and so an error comes out that interrupts the regular flow of the program. Am I sure that the destructor of the class is called when an exception happens inside his method?

In other words, when the exception occurs inside a method of the class I need to free the heap allocated memory. I am doing it in the destructor but is this called when the exception occurs?


I have seen hundreds of answers here saying that the destructor is not called if an exception occurs in the constructor but this is not my case. I'm inside a method! C++ Primer and google tell me to use smart pointers and I am ok, but I want to understand how this work so I need to know this info.

Emma Rossignoli
  • 935
  • 7
  • 25
  • It's called RAII – Guillaume Racicot Dec 05 '17 at 18:21
  • As `test` is fully constructed then the destructor of `test` will be called when `test` goes out of scope. You should post a [mcve] as your code sample does not allow me to say when this happens. – Richard Critten Dec 05 '17 at 18:22
  • @RichardCritten "when test goes out of scope" -> it goes out of scope once the exception has been caught/the program exited? – Emma Rossignoli Dec 05 '17 at 18:23
  • @EmmaRossignoli You need to post a bit more code - it happens after the last `}` in your 2nd code sample. You haven't posted a "program" for us to comment on. – Richard Critten Dec 05 '17 at 18:24
  • @EmmaRossignoli Since you are `catch`ing the exception, and the instance of `A` is declared in the scope above `try` block - it wouldn't go out of scope, once the exception is caught. – Algirdas Preidžius Dec 05 '17 at 18:25
  • @AlgirdasPreidžius I think that I have got it. Without the try - catch the program would have crashed --> stack is freed --> A goes out of scope --> A destructor is called and I'm happy. Is that correct? – Emma Rossignoli Dec 05 '17 at 18:27
  • 1
    @EmmaRossignoli not really; when there's a crash, the stack isn't unwound at all. What happens is when you catch, it allows the program to unwind the stack as it needs to, and then continue. Anything that lost scope as it was unwound is freed with destructors called. – UKMonkey Dec 05 '17 at 18:30
  • 1
    @EmmaRossignoli "_Without the try - catch the program would have <...>_" Even with try - catch, A would be destructed _eventually_ (once the code execution left the scope where `test` is declared in). I commented about your statements regarding it being destructed once the exception was thrown/caught. – Algirdas Preidžius Dec 05 '17 at 18:30

0 Answers0