Are there any situations where a class destructor would not be called for a class when it leaves the scope?
Asked
Active
Viewed 160 times
0
-
What do you mean by *when being cleaned up*? Do you mean when the scope ends? When you throw an exception? – NathanOliver Oct 19 '21 at 20:56
-
@NathanOliver When the scope ends – Enigma Oct 19 '21 at 20:57
-
related/dupe: https://stackoverflow.com/questions/27741698/is-c-compiler-allowed-to-optimize-out-unreferenced-local-objects – NathanOliver Oct 19 '21 at 21:01
-
1Terminating the program. Also `longjmp()`. – HolyBlackCat Oct 19 '21 at 21:04
-
1Placement new... – StoryTeller - Unslander Monica Oct 19 '21 at 21:15
1 Answers
3
Well, assuming that the object here is plain value (not member of union or allocated in heap so cannot be leaked), I can imagine only one situation: ending a process by something like std::terminate.
It can be called directly or called in situations when uncatched exception occurs in noexcept
method.
It may be not "correct" to call this situation an "end of the scope" because you never actually return from std::terminate. However, from practical point of view this is probably exact situation which you want to know about.

Angelicos Phosphoros
- 2,469
- 9
- 24