https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118865
--- Comment #7 from scott snyder <snyder at bnl dot gov> --- Thanks Andrew! It appears that i was too aggressive in reducing the original source, which as far as i can tell does handle empty containers properly. For the record, it can be seen here (not my code, BTW...): https://gitlab.cern.ch/atlas/athena/-/blob/main/Reconstruction/Jet/JetMonitoring/src/JetHistoResponseAndEff.cxx?ref_type=heads#L65 Here's another attempt at reducing this that should handle properly the case of empty input containers: -- test2.cc ------------------------------------------------------------ #include <list> #include <cmath> void foo(double); double dist (int, int); void processJetContainer(const int* jbeg, const int* jend, const int* rbeg, const int* rend) { using lint = std::list<int>; if (jbeg == jend) return; lint listJets(jbeg, jend); for (; rbeg < rend; ++rbeg) { double dr2min = 500000; if (listJets.empty() ) break; lint::iterator it=listJets.begin(); lint::iterator itmin=it; for( ; it != listJets.end(); ++it) { double dr2 = dist(*it, *rbeg); if(dr2 < dr2min) { dr2min = dr2; itmin = it ;} } listJets.erase(itmin); foo (sqrt(dr2min)); } } ------------------------------------------------------------------------ This does not warn with gcc14, but does with gcc15, even with your proposed patch. $ g++ -c x.cc -O1 In file included from /usr/local/gcc/include/c++/15.0.1/x86_64-pc-linux-gnu/bits/c++allocator.h:33, from /usr/local/gcc/include/c++/15.0.1/bits/allocator.h:46, from /usr/local/gcc/include/c++/15.0.1/list:65, from x.cc:1: In member function ‘void std::__new_allocator<_Tp>::deallocate(_Tp*, size_type) [with _Tp = std::_List_node<int>]’, inlined from ‘static void std::allocator_traits<std::allocator<_Tp1> >::deallocate(allocator_type&, pointer, size_type) [with _Tp = std::_List_node<int>]’ at /usr/local/gcc/include/c++/15.0.1/bits/alloc_traits.h:649:23, inlined from ‘void std::__cxx11::_List_base<_Tp, _Alloc>::_M_put_node(_Node_ptr) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/local/gcc/include/c++/15.0.1/bits/stl_list.h:820:32, inlined from ‘void std::__cxx11::_List_base<_Tp, _Alloc>::_M_destroy_node(_Node_ptr) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/local/gcc/include/c++/15.0.1/bits/stl_list.h:855:19, inlined from ‘void std::__cxx11::list<_Tp, _Allocator>::_M_erase(iterator) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/local/gcc/include/c++/15.0.1/bits/stl_list.h:2485:23, inlined from ‘std::__cxx11::list<_Tp, _Allocator>::iterator std::__cxx11::list<_Tp, _Allocator>::erase(const_iterator) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/local/gcc/include/c++/15.0.1/bits/list.tcc:153:15, inlined from ‘void processJetContainer(const int*, const int*, const int*, const int*)’ at x.cc:27:19: /usr/local/gcc/include/c++/15.0.1/bits/new_allocator.h:172:66: warning: ‘void operator delete(void*, std::size_t)’ called on unallocated object ‘listJets’ [-Wfree-nonheap-object] 172 | _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n)); | ^ x.cc: In function ‘void processJetContainer(const int*, const int*, const int*, const int*)’: x.cc:13:8: note: declared here 13 | lint listJets(jbeg, jend); | ^~~~~~~~ $