https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89303
--- Comment #6 from Alexandre Duret-Lutz <adl at gnu dot org> --- I mentioned in my first comment that I had also cases that threw bad_weak_ptr. I've been able to reproduce those as follows: % cat badwptr.cc #include <memory> class blob final: public std::enable_shared_from_this<blob> { }; int main() { std::shared_ptr<blob> tg = std::make_shared<blob>(); return tg->shared_from_this().use_count(); } % g++ -g -O badwptr.cc % ./a.out terminate called after throwing an instance of 'std::bad_weak_ptr' what(): bad_weak_ptr When compiled with -O2 or -O0, the program exits with $?=2 as expected.