From 0721d0b84454b902eef2ff3824dd2dc67b413f9a Mon Sep 17 00:00:00 2001
From: sss <sss@karma>
Date: Sun, 8 Dec 2024 21:15:59 -0500
Subject: [PATCH 1/2] MuPatPrimitives: Lifetime management update

Have addToTrash return a (bare) pointer to the object just added.
Avoids having a pattern in the caller where it looks like a dangling
pointer is retained from a unique_ptr.
---
 .../MuPatPrimitives/MuPatPrimitives/MuPatCandidateBase.h | 4 ++--
 .../MuPatPrimitives/src/MuPatCandidateBase.cxx           | 9 +++++++--
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/MuPatCandidateBase.h b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/MuPatCandidateBase.h
index 2b12809f1609..eb3a306f3103 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/MuPatCandidateBase.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/MuPatCandidateBase.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef MUPATCANDIDATEBASE_H
@@ -114,7 +114,7 @@ namespace Muon {
         bool shareChambers(const MuPatCandidateBase& entry) const;
 
         /** @brief adds the measurement to the garbage container. */
-        void addToTrash(std::unique_ptr<const Trk::MeasurementBase> meas);
+        const Trk::MeasurementBase* addToTrash(std::unique_ptr<const Trk::MeasurementBase> meas);
         void addToTrash(const std::vector<std::shared_ptr<const Trk::MeasurementBase>>& measurements);
         const std::vector<std::shared_ptr<const Trk::MeasurementBase>>& garbage() const;
     protected:
diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/src/MuPatCandidateBase.cxx b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/src/MuPatCandidateBase.cxx
index 248058e9ae13..6e701db43d6a 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/src/MuPatCandidateBase.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/src/MuPatCandidateBase.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 #include "MuPatPrimitives/MuPatCandidateBase.h"
@@ -88,7 +88,12 @@ namespace Muon {
 
     bool MuPatCandidateBase::hasMomentum() const { return m_hasMomentum; }
 
-    void MuPatCandidateBase::addToTrash(std::unique_ptr<const Trk::MeasurementBase> meas) {m_garbage.push_back(std::move(meas));}
+    const Trk::MeasurementBase*
+    MuPatCandidateBase::addToTrash(std::unique_ptr<const Trk::MeasurementBase> meas)
+    {
+      m_garbage.push_back(std::move(meas));
+      return m_garbage.back().get();
+    }
     void MuPatCandidateBase::addToTrash(const std::vector<std::shared_ptr<const Trk::MeasurementBase>>& measurements) {
         if (m_garbage.capacity() < measurements.size() + m_garbage.size()){
             m_garbage.reserve(measurements.size() + m_garbage.size());
-- 
GitLab


From b9de08f7ddf7470ecc47f340482313f93ad27162 Mon Sep 17 00:00:00 2001
From: sss <sss@karma>
Date: Sun, 26 Jan 2025 17:27:19 -0500
Subject: [PATCH 2/2] MuonTrackSteeringTools: Clean up cppcheck warning
 suppression

Have addToTrash return a (bare) pointer to the object just added.
Avoids having a pattern in the caller where it looks like a dangling
pointer is retained from a unique_ptr.

Allows getting rid of a cppcheck warning suppression.
---
 .../src/MuPatCandidateTool.cxx                     | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
index 628bad210dd6..87494fb08904 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatCandidateTool.cxx
@@ -153,8 +153,6 @@ namespace Muon {
                 fakePhiHits.push_back(meas);
                 continue;
             }
-            std::unique_ptr<Trk::MeasurementBase> newRot{};
-
 
             // skip ID hits
             if (!m_idHelperSvc->isMuon(id)) continue;
@@ -187,8 +185,9 @@ namespace Muon {
                             continue;
                         }
                         ATH_MSG_DEBUG(" recreating MdtDriftCircleOnTrack ");
-                        newRot = std::unique_ptr<MdtDriftCircleOnTrack>{m_mdtRotCreator->createRIO_OnTrack(*mdt->prepRawData(), mdt->globalPosition())};
-                        meas = newRot.get();
+                        meas = entry.addToTrash
+                          (std::unique_ptr<const MdtDriftCircleOnTrack>
+                           (m_mdtRotCreator->createRIO_OnTrack(*mdt->prepRawData(), mdt->globalPosition())));
                     }
                 }
 
@@ -225,8 +224,9 @@ namespace Muon {
                         continue;
                     }
                     ATH_MSG_DEBUG(" recreating CscClusterOnTrack ");
-                    newRot = std::unique_ptr<MuonClusterOnTrack>{m_cscRotCreator->createRIO_OnTrack(*csc->prepRawData(), csc->globalPosition())};
-                    meas = newRot.get();
+                    meas = entry.addToTrash
+                      (std::unique_ptr<const MuonClusterOnTrack>
+                       (m_cscRotCreator->createRIO_OnTrack(*csc->prepRawData(), csc->globalPosition())));
                 }  
                 
 
@@ -249,8 +249,6 @@ namespace Muon {
             } else {
                 etaHits.push_back(meas);
             }
-            // cppcheck-suppress nullPointerRedundantCheck; false positive
-            entry.addToTrash(std::move(newRot));
         }
 
         if (createComp) {
-- 
GitLab