From 0943a0c35f9972b6d004c1b5e4c2c701829f5f31 Mon Sep 17 00:00:00 2001
From: scott snyder <snyder@bnl.gov>
Date: Tue, 18 Feb 2025 17:22:25 +0100
Subject: [PATCH] GeneratorObjectsTPCnv: Fix use of invalidated iterators.

Reverts previous incorrect change.
Fixes test failures in the hepmc2 build.
---
 .../src/initMcEventCollection.cxx                     | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/Generators/GeneratorObjectsTPCnv/src/initMcEventCollection.cxx b/Generators/GeneratorObjectsTPCnv/src/initMcEventCollection.cxx
index a301a095dc40..e920a7fce4ee 100644
--- a/Generators/GeneratorObjectsTPCnv/src/initMcEventCollection.cxx
+++ b/Generators/GeneratorObjectsTPCnv/src/initMcEventCollection.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 #include "GeneratorObjectsTPCnv/initMcEventCollection.h"
 
@@ -150,9 +150,12 @@ namespace Athena_test {
     }
 #else
     if (!ge.vertices_empty()) {
-      for (auto itvtx = ge.vertices_begin(); itvtx != ge.vertices_end();
-           ++itvtx) {
-        ge.remove_vertex(*itvtx);
+      HepMC::GenEvent::vertex_iterator itvtx = ge.vertices_begin();
+      while (itvtx != ge.vertices_end()) {
+        HepMC::GenVertexPtr pvtx = *itvtx;
+        ++itvtx;
+        ge.remove_vertex(pvtx);
+        delete pvtx;
       }
     }
 #endif
-- 
GitLab