From c1caf5ebadbf0c374f970560949ad4bba7e1b37d Mon Sep 17 00:00:00 2001 From: Te-Chuan Huang Date: Fri, 6 Oct 2023 08:52:43 -0500 Subject: [PATCH] Fix memory leak in StFstClusterMaker (#593) Memory leaks were found by Gene in issue #590. There are 3 places causing the leak in `StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx`: 1. Pointers assigned to std::vector in `line 55` were not deleted before calling std::vector::pop_back() in `line 69` and `line 80`. 2. Object created in `line 140` but only conditionally assigned to a std::vector in `line 148`. 3. In `line 203`, std::vector::erase() was called without deleting the pointers. After fixing these places, there is no memory leak from StFstClusterMaker reported by valgrind. --------- Co-authored-by: Dmitri Smirnov Co-authored-by: Te-Chuan Huang --- .../StFstScanRadiusClusterAlgo.cxx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/StRoot/StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx b/StRoot/StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx index af418cde498..c47344cc4db 100644 --- a/StRoot/StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx +++ b/StRoot/StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx @@ -66,6 +66,7 @@ Int_t StFstScanRadiusClusterAlgo::doClustering(const StFstCollection &fstCollect while ( !rawHitsVec[sensorIdx][phiIdx].empty() ) { rawHitTemp = rawHitsVec[sensorIdx][phiIdx].back(); + delete rawHitsVec[sensorIdx][phiIdx].back(); rawHitsVec[sensorIdx][phiIdx].pop_back(); rawHitsToMerge.push_back(rawHitTemp); //count number to merge @@ -77,6 +78,7 @@ Int_t StFstScanRadiusClusterAlgo::doClustering(const StFstCollection &fstCollect // put all raw hits in one phi strip to rawHitsToMerge while (rawHitsToMergePtr != rawHitsToMerge.end() && !rawHitsVec[sensorIdx][phiIdx].empty()) { rawHitTemp = rawHitsVec[sensorIdx][phiIdx].back(); + delete rawHitsVec[sensorIdx][phiIdx].back(); rawHitsVec[sensorIdx][phiIdx].pop_back(); ++nToMerge; @@ -137,14 +139,14 @@ Int_t StFstScanRadiusClusterAlgo::doClustering(const StFstCollection &fstCollect totCharge = tempSumCharge; totChargeErr = sqrt(tempSumChargeErrSquare / nToMerge); - newCluster = new StFstCluster((int)wedge * 10000 + clusterLabel, disk, wedge, sensor, apv, meanRStrip, meanPhiStrip, totCharge, totChargeErr, clusterType); - newCluster->setNRawHits(clusterSize); - newCluster->setNRawHitsR(clusterSizeR); - newCluster->setNRawHitsPhi(clusterSizePhi); - newCluster->setMaxTimeBin(maxTb); - newCluster->setIdTruth(idTruth); - if(nToSeedhit>0) { + newCluster = new StFstCluster((int)wedge * 10000 + clusterLabel, disk, wedge, sensor, apv, meanRStrip, meanPhiStrip, totCharge, totChargeErr, clusterType); + newCluster->setNRawHits(clusterSize); + newCluster->setNRawHitsR(clusterSizeR); + newCluster->setNRawHitsPhi(clusterSizePhi); + newCluster->setMaxTimeBin(maxTb); + newCluster->setIdTruth(idTruth); + clustersVec[sensorIdx][phiIdx].push_back(newCluster); clusterLabel++; } @@ -200,6 +202,7 @@ Int_t StFstScanRadiusClusterAlgo::doClustering(const StFstCollection &fstCollect (*clusterIt2)->setApv(apv); int distance1 = std::distance(clustersVec[sensorIdx][phiIdx1].begin(), clusterIt1); + delete *clusterIt1; clustersVec[sensorIdx][phiIdx1].erase(clusterIt1); if (distance1 == 0)