Skip to content

Commit

Permalink
Fix leak from HepPDT::HeavyIonUnknownID
Browse files Browse the repository at this point in the history
HepPDT::HeavyIonUnknownID creates a new particle in the case the
HepPDT does not know of that particle by default. However, nothing
deletes that particle. This code wraps HeavyIonUnknownID and does
a thread safe cache of the particle in order to delete it later.
  • Loading branch information
Dr15Jones committed Nov 19, 2018
1 parent c6061f4 commit 0b30d94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions SimGeneral/HepPDTESSource/BuildFile.xml
@@ -1,3 +1,4 @@
<use name="FWCore/ParameterSet"/>
<use name="SimGeneral/HepPDTRecord"/>
<use name="tbb"/>
<flags EDM_PLUGIN="1"/>
25 changes: 24 additions & 1 deletion SimGeneral/HepPDTESSource/src/HepPDTESSource.cc
@@ -1,5 +1,28 @@
#include "SimGeneral/HepPDTESSource/interface/HepPDTESSource.h"
#include "HepPDT/HeavyIonUnknownID.hh"
#include "tbb/concurrent_unordered_map.h"

namespace {

class CachingHeavyIonUnknownID : public HepPDT::ProcessUnknownID {
HepPDT::ParticleData * processUnknownID( HepPDT::ParticleID id,
const HepPDT::ParticleDataTable & table) final {
auto find = idToParticleMap_.find(id.pid());
if(find != idToParticleMap_.end()) {
return find->second.get();
}

//HeavyIonUnknownID constructs a new particle but does not delete it
// we need to do that ourselves
return idToParticleMap_.emplace( id.pid(), std::unique_ptr<HepPDT::ParticleData>{ wrapped_.callProcessUnknownID(id, table) } ).first->second.get();

}

HepPDT::HeavyIonUnknownID wrapped_;
tbb::concurrent_unordered_map<int, std::unique_ptr<HepPDT::ParticleData>> idToParticleMap_;
};

}

HepPDTESSource::HepPDTESSource( const edm::ParameterSet& cfg ) :
pdtFileName( cfg.getParameter<edm::FileInPath>( "pdtFileName" ) ) {
Expand All @@ -13,7 +36,7 @@ HepPDTESSource::~HepPDTESSource() {
HepPDTESSource::ReturnType
HepPDTESSource::produce( const PDTRecord & iRecord ) {
using namespace edm::es;
auto pdt = std::make_unique<PDT>( "PDG table" , new HepPDT::HeavyIonUnknownID );
auto pdt = std::make_unique<PDT>( "PDG table" , new CachingHeavyIonUnknownID );
std::ifstream pdtFile( pdtFileName.fullPath().c_str() );
if( ! pdtFile )
throw cms::Exception( "FileNotFound", "can't open pdt file" )
Expand Down

0 comments on commit 0b30d94

Please sign in to comment.