Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I jump out of a catch-block with "goto", am I guaranteed that the exception-object will be free'ed?

I have such code as follows

try {
  doSomething();
} catch(InterruptException) {
  goto rewind_code;
}

if(0) {
rewind_code:
  longjmp(savepoint, 1);
}

My question is, is the exception object that is stored by the C++ runtime free'ed when I goto out of the catch block? Or is the runtime allowed to cache it until the surrounding function exists or something like that? I simply want to ensure that if I execute above code multiple times, each time taking the rewind code, I won't leak memory (because the longjmp won't execute cleanup code emitted by the compiler into or before function prologues).

like image 942
Johannes Schaub - litb Avatar asked Aug 31 '11 20:08

Johannes Schaub - litb


1 Answers

§6.6/2:

On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration...

At least as I'd read it, "however accomplished" should/does include a goto.

Edit: Okay, based on Johannes's comment, what we care about is §15.1/4:

When the last handler being executed for the exception exits by any means other than throw; the temporary object is destroyed and the implementation may deallocate the memory for the temporary object;

[ ... ]

The destruction occurs immediately after the destruction of the object declared in the exception-declaration in the handler.

like image 59
Jerry Coffin Avatar answered Sep 28 '22 13:09

Jerry Coffin