From 89da29eb6b020d5ae7548287e95c6da6984a522c Mon Sep 17 00:00:00 2001
From: scott snyder <snyder@bnl.gov>
Date: Tue, 22 Dec 2020 00:03:56 +0100
Subject: [PATCH] MuonTrackSteeringTools: Another attempt to fix dangling
 Surface references.

finalize() is still to late to clean up the garbage in MuPatCandidateTool,
as we also clean stores in step.  Try moving deleting the garbage
to stop().

See ATLASRECTS-5843.
---
 .../MuonTrackSteeringTools/src/MuPatCandidateTool.cxx    | 6 +++++-
 .../MuonTrackSteeringTools/src/MuPatCandidateTool.h      | 9 ++++++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
index 4bf8325d4af..d12909b845c 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
@@ -58,9 +58,13 @@ namespace Muon {
   }
 
   StatusCode MuPatCandidateTool::finalize() {
+    return StatusCode::SUCCESS;
+  }
+
+  StatusCode MuPatCandidateTool::stop() {
 
     // Clean up all garbage now.
-    // If we leave it for the dtor, we may end up with dangling references
+    // If we leave it for later, we may end up with dangling references
     // to Surface objects that have already been deleted.
     for (CacheEntry& ent : m_cache) {
       ent.cleanUp();
diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.h b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.h
index 9408b03df00..da0186c3a72 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.h
@@ -65,13 +65,16 @@ namespace Muon {
     MuPatCandidateTool(const std::string&, const std::string&, const IInterface*);
     
     /** destructor */
-    ~MuPatCandidateTool() = default;
+    virtual ~MuPatCandidateTool() = default;
     
     /** initialize method, method taken from bass-class AlgTool */
-    StatusCode initialize();
+    virtual StatusCode initialize() override;
 
     /** finialize method, method taken from bass-class AlgTool */
-    StatusCode finalize();
+    virtual StatusCode finalize() override;
+
+    /** stop method, used to clean up garbage */
+    virtual StatusCode stop() override;
     
     /** @brief access to tool interface */
     static const InterfaceID& interfaceID() { return IID_MuPatCandidateTool; }
-- 
GitLab