Skip to content

Commit

Permalink
Fix memory leak in StFstClusterMaker (star-bnl#593)
Browse files Browse the repository at this point in the history
Memory leaks were found by Gene in issue star-bnl#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 <[email protected]>
Co-authored-by: Te-Chuan Huang <[email protected]>
  • Loading branch information
3 people authored and dkapukchyan committed Mar 11, 2024
1 parent 8aa96e0 commit a93a362
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions StRoot/StFstClusterMaker/StFstScanRadiusClusterAlgo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a93a362

Please sign in to comment.