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

后端 未结 2 573
清歌不尽
清歌不尽 2021-02-13 13:47

I have such code as follows

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

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


        
相关标签:
2条回答
  • 2021-02-13 14:29

    §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.

    0 讨论(0)
  • 2021-02-13 14:37

    § 15.1.4

    The memory for the exception object is allocated in an unspecified way, except as noted in 3.7.4.1. If a handler exits by rethrowing, control is passed to another handler for the same exception. The exception object is destroyed after either the last remaining active handler for the exception exits by any means other than rethrowing, or the last object of type std::exception_ptr (18.8.5) that refers to the exception object is destroyed, whichever is later. In the former case, the destruction occurs when the handler exits, immediately after the destruction of the object declared in the exception-declaration in the handler, if any. In the latter case, the destruction occurs before the destructor of std::exception_ptr returns. The implementation may then deallocate the memory for the exception object; any such deallocation is done in an unspecified way.

    0 讨论(0)
提交回复
热议问题