r/cpp • u/User_Deprecated • Jul 11 '26
Propagating exceptions from destructors with std::exception_ptr
https://www.sandordargo.com/blog/2026/07/08/exception_ptr
80
Upvotes
r/cpp • u/User_Deprecated • Jul 11 '26
View all comments
Show parent comment
7
u/kammce WG21 | πΊπ² NB | Boost | Exceptions Jul 11 '26
Im pretty sure it's UB to kill a thread that hasn't finished. If your thread is currently within a catch block, then it's at that point where it needs to use current_exception and get a copy of the exception_ptr. Once you have e that copy, you have ref_count = 2, and when the code exits the catch block, you now have ref_count = 1, because the TLS slot got wiped out. If you pass that ptr to another thread, somehow, it'll have access to that exception. From there, if your thread exits and then is destroyed, that's fine, the TLS slot for the current exception wasn't being used anymore anyway.