From c1ac5666b63b7a8a960f38d82f7a8734bb384571 Mon Sep 17 00:00:00 2001 From: Andre Sailer <andre.philippe.sailer@cern.ch> Date: Thu, 23 Jun 2022 10:23:01 +0200 Subject: [PATCH] EDM4hepOutput: correctly treat re-used collections emplace doesn't overwrite already existing collection, so this would lead to leaking memory --- DDG4/edm4hep/Geant4Output2EDM4hep.cpp | 49 ++++++++++++++++----------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/DDG4/edm4hep/Geant4Output2EDM4hep.cpp b/DDG4/edm4hep/Geant4Output2EDM4hep.cpp index b40226540..2bafbdbd1 100644 --- a/DDG4/edm4hep/Geant4Output2EDM4hep.cpp +++ b/DDG4/edm4hep/Geant4Output2EDM4hep.cpp @@ -565,29 +565,40 @@ void Geant4Output2EDM4hep::createCollections(OutputContext<G4Event>& ctxt){ if( typeid( Geant4Tracker::Hit ) == coll->type().type() ){ auto* sthc = new edm4hep::SimTrackerHitCollection(); - m_collections.emplace(colName, sthc); - m_store->registerCollection(colName, sthc); - m_file->registerForWrite(colName); - auto& sthc_md = m_store->getCollectionMetaData( sthc->getID() ); - sthc_md.setValue("CellIDEncodingString", sd_enc); - printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); + auto pairColCheck = m_collections.emplace(colName, sthc); + if ( pairColCheck.second ) { + m_store->registerCollection(colName, sthc); + m_file->registerForWrite(colName); + auto& sthc_md = m_store->getCollectionMetaData( sthc->getID() ); + sthc_md.setValue("CellIDEncodingString", sd_enc); + printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); + } else { + delete sthc; + } + printout(DEBUG,"Geant4Output2EDM4hep","+++ re-using collection %s",colName.c_str() ); + } else if( typeid( Geant4Calorimeter::Hit ) == coll->type().type() ){ auto* schc = new edm4hep::SimCalorimeterHitCollection(); - m_collections.emplace(colName, schc); - m_store->registerCollection(colName, schc); - m_file->registerForWrite(colName); - auto& schc_md = m_store->getCollectionMetaData( schc->getID() ); - schc_md.setValue("CellIDEncodingString", sd_enc); - printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); - - colName += "Contributions" ; - auto* chContribColl = new edm4hep::CaloHitContributionCollection(); - m_collections.emplace(colName, chContribColl); - m_store->registerCollection(colName, chContribColl); - m_file->registerForWrite(colName); - printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); + auto pairColCheck = m_collections.emplace(colName, schc); + if ( pairColCheck.second ) { + m_store->registerCollection(colName, schc); + m_file->registerForWrite(colName); + auto& schc_md = m_store->getCollectionMetaData( schc->getID() ); + schc_md.setValue("CellIDEncodingString", sd_enc); + printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); + + colName += "Contributions" ; + auto* chContribColl = new edm4hep::CaloHitContributionCollection(); + m_collections.emplace(colName, chContribColl); + m_store->registerCollection(colName, chContribColl); + m_file->registerForWrite(colName); + printout(DEBUG,"Geant4Output2EDM4hep","+++ created collection %s",colName.c_str() ); + } else { + delete schc; + printout(DEBUG,"Geant4Output2EDM4hep","+++ re-using collection %s",colName.c_str() ); + } } else { -- GitLab