From b98eaaf82c7ce327d0f59b7ea316b44ee8678a0a Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Mon, 18 Nov 2024 10:29:20 -0800 Subject: [PATCH] Fix warning and undefined behavior (#4236) * Move instead of copy. * std::vector::erase invalidates the iterator, thus the original code has undefined behavior in principle. --- Src/Base/AMReX_VisMF.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Base/AMReX_VisMF.cpp b/Src/Base/AMReX_VisMF.cpp index 6749c626c3..8b9a80d0d3 100644 --- a/Src/Base/AMReX_VisMF.cpp +++ b/Src/Base/AMReX_VisMF.cpp @@ -75,14 +75,14 @@ namespace auto cached_ba = it->first.lock(); if (cached_ba) { if (cached_ba->m_abox == ba_ref->m_abox) { - ba_ref = cached_ba; + ba_ref = std::move(cached_ba); dm_ref = it->second.lock(); ielem = int(std::distance(s_layout_cache.begin(), it)); break; } ++it; } else { // expired - s_layout_cache.erase(it); + it = s_layout_cache.erase(it); } }