From 0edc724b3e56e55564ea7413dca1c2e9645ad463 Mon Sep 17 00:00:00 2001
From: Sergey Gorbunov <se.gorbunov@gsi.de>
Date: Thu, 27 Oct 2022 12:37:03 +0000
Subject: [PATCH] sts sensor: store CbmLink instead of a pointer to CbmLink.
 Fixes memory leak.

---
 sim/detectors/sts/CbmStsDigitize.cxx      |  5 ++---
 sim/detectors/sts/CbmStsDigitize.h        |  2 +-
 sim/detectors/sts/CbmStsSimSensor.cxx     |  2 +-
 sim/detectors/sts/CbmStsSimSensor.h       |  8 +++++---
 sim/detectors/sts/CbmStsSimSensorDssd.cxx | 11 +++--------
 5 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/sim/detectors/sts/CbmStsDigitize.cxx b/sim/detectors/sts/CbmStsDigitize.cxx
index c9619a01ff..5571246d0c 100644
--- a/sim/detectors/sts/CbmStsDigitize.cxx
+++ b/sim/detectors/sts/CbmStsDigitize.cxx
@@ -613,7 +613,6 @@ void CbmStsDigitize::ProcessMCEvent()
   assert(fPoints);
   for (Int_t iPoint = 0; iPoint < fPoints->GetEntriesFast(); iPoint++) {
     const CbmStsPoint* point = (const CbmStsPoint*) fPoints->At(iPoint);
-    CbmLink* link            = new CbmLink(1., iPoint, fCurrentMCEntry, fCurrentInput);
 
     // --- Ignore points from secondaries if the respective flag is set
     if (fParSim->OnlyPrimaries()) {
@@ -639,16 +638,16 @@ void CbmStsDigitize::ProcessMCEvent()
     }
 
     // --- Process the StsPoint
+    CbmLink link(1., iPoint, fCurrentMCEntry, fCurrentInput);
     ProcessPoint(point, fCurrentEventTime, link);
     fNofPointsProc++;
-    delete link;
   }  //# StsPoints
 }
 // -------------------------------------------------------------------------
 
 
 // -----  Process a StsPoint   ---------------------------------------------
-void CbmStsDigitize::ProcessPoint(const CbmStsPoint* point, Double_t eventTime, CbmLink* link)
+void CbmStsDigitize::ProcessPoint(const CbmStsPoint* point, Double_t eventTime, const CbmLink& link)
 {
 
   // --- Get the sensor the point is in
diff --git a/sim/detectors/sts/CbmStsDigitize.h b/sim/detectors/sts/CbmStsDigitize.h
index 5b71efdbab..5679ac6cac 100644
--- a/sim/detectors/sts/CbmStsDigitize.h
+++ b/sim/detectors/sts/CbmStsDigitize.h
@@ -349,7 +349,7 @@ private:
    ** @param point  Pointer to CbmStsPoint to be processed
    ** @param link   Link to MCPoint
    **/
-  void ProcessPoint(const CbmStsPoint* point, Double_t eventTime = 0., CbmLink* link = NULL);
+  void ProcessPoint(const CbmStsPoint* point, Double_t eventTime, const CbmLink& link);
 
 
   /** @brief Reset event counters **/
diff --git a/sim/detectors/sts/CbmStsSimSensor.cxx b/sim/detectors/sts/CbmStsSimSensor.cxx
index 2f373d9271..ee4b13f8d9 100644
--- a/sim/detectors/sts/CbmStsSimSensor.cxx
+++ b/sim/detectors/sts/CbmStsSimSensor.cxx
@@ -58,7 +58,7 @@ Int_t CbmStsSimSensor::GetSensorId() const
 
 
 // -----   Process a CbmStsPoint  ------------------------------------------
-Int_t CbmStsSimSensor::ProcessPoint(const CbmStsPoint* point, Double_t eventTime, CbmLink* link)
+Int_t CbmStsSimSensor::ProcessPoint(const CbmStsPoint* point, Double_t eventTime, const CbmLink& link)
 {
 
   // --- Physical node
diff --git a/sim/detectors/sts/CbmStsSimSensor.h b/sim/detectors/sts/CbmStsSimSensor.h
index 78fb3dfdc0..f3e1d581bd 100644
--- a/sim/detectors/sts/CbmStsSimSensor.h
+++ b/sim/detectors/sts/CbmStsSimSensor.h
@@ -11,6 +11,8 @@
 #define CBMSTSSIMSENSOR_H 1
 
 
+#include "CbmLink.h"
+
 #include <TObject.h>
 #include <TString.h>
 
@@ -70,7 +72,7 @@ public:
   /** @brief Current link object
      ** @return Current link object (to CbmStsPoint)
      **/
-  CbmLink* GetCurrentLink() const { return fCurrentLink; }
+  const CbmLink& GetCurrentLink() const { return fCurrentLink; }
 
 
   /** @brief Corresponding element in STS setup
@@ -107,7 +109,7 @@ public:
      ** system. The appropriate analogue response is then calculated
      ** with the pure virtual method CalculateResponse.
      **/
-  Int_t ProcessPoint(const CbmStsPoint* point, Double_t eventTime = 0., CbmLink* link = NULL);
+  Int_t ProcessPoint(const CbmStsPoint* point, Double_t eventTime, const CbmLink& link);
 
 
   /** Set the sensor conditions
@@ -172,7 +174,7 @@ protected:
   Double_t fBx                           = 0.;       ///< x component of magnetic field in sensor centre
   Double_t fBy                           = 0.;       ///< y component of magnetic field in sensor centre
   Double_t fBz                           = 0.;       ///< z component of magnetic field in sensor centre
-  CbmLink* fCurrentLink                  = nullptr;  //! Link to currently processed MCPoint
+  CbmLink fCurrentLink                   = {};       //! Link to currently processed MCPoint
 
 
   /** Perform response simulation for one MC Point
diff --git a/sim/detectors/sts/CbmStsSimSensorDssd.cxx b/sim/detectors/sts/CbmStsSimSensorDssd.cxx
index fc109f7f7f..8c7cc0ce46 100644
--- a/sim/detectors/sts/CbmStsSimSensorDssd.cxx
+++ b/sim/detectors/sts/CbmStsSimSensorDssd.cxx
@@ -280,14 +280,9 @@ void CbmStsSimSensorDssd::RegisterCharge(Int_t side, Int_t strip, Double_t charg
   Int_t channel = GetModuleChannel(strip, side, GetSensorId());
 
   // --- Get the MC link information
-  Int_t index = -1;
-  Int_t entry = -1;
-  Int_t file  = -1;
-  if (GetCurrentLink()) {
-    index = GetCurrentLink()->GetIndex();
-    entry = GetCurrentLink()->GetEntry();
-    file  = GetCurrentLink()->GetFile();
-  }
+  Int_t index = GetCurrentLink().GetIndex();
+  Int_t entry = GetCurrentLink().GetEntry();
+  Int_t file  = GetCurrentLink().GetFile();
 
   // --- Send signal to module
   GetModule()->AddSignal(channel, time, charge, index, entry, file);
-- 
GitLab