https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118854
--- Comment #13 from Nicholas Williams <nicholas at nicholaswilliams dot net> --- (In reply to Jonathan Wakely from comment #10) > If you need to make copies of a shared_ptr while also modifying it, you need > to use std::atomic<std::shared_ptr<T>> (or > std::experimental::atomic_shared_ptr). Using std::atomic<std::shared_ptr> results in the same race condition heap-use-after-free while copying the shared_ptr: ... #0 0x40a582 in __gnu_cxx::__atomic_add(int volatile*, int) /opt/rh/gcc-toolset-14/root/usr/include/c++/14/ext/atomicity.h:71 #1 0x40a582 in __gnu_cxx::__atomic_add_dispatch(int*, int) /opt/rh/gcc-toolset-14/root/usr/include/c++/14/ext/atomicity.h:111 #2 0x40a582 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_add_ref_copy() /opt/rh/gcc-toolset-14/root/usr/include/c++/14/bits/shared_ptr_base.h:152 #3 0x409cd6 in std::_Sp_atomic<std::shared_ptr<Resource> >::_S_add_ref(std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>*) /opt/rh/gcc-toolset-14/root/usr/include/c++/14/bits/shared_ptr_atomic.h:536 #4 0x4090d3 in std::_Sp_atomic<std::shared_ptr<Resource> >::load(std::memory_order) const /opt/rh/gcc-toolset-14/root/usr/include/c++/14/bits/shared_ptr_atomic.h:568 #5 0x407874 in std::atomic<std::shared_ptr<Resource> >::load(std::memory_order) const /opt/rh/gcc-toolset-14/root/usr/include/c++/14/bits/shared_ptr_atomic.h:656 ... (In reply to Jonathan Wakely from comment #12) > ***any of those accesses uses a non-const member function of shared_ptr then > a data race will occur*** shared_ptr::shared_ptr( shared_ptr const & other ); By definition, uses of `other` should exclusively use only const members, not non-const members, of `other`, because `other` is declared `const`.