Memory usage in G4HadronicInteraction::ApplyYourself()

G4HadronElastic.cc contains:

G4DynamicParticle * aSec = new G4DynamicParticle(theDef, lv.vect().unit(), erec); theParticleChange.AddSecondary(aSec, secID);
// which then does theSecs.push_back(G4HadSecondary(aP, theW, mod));

But ClearSecondaries() only does:
theSecs.clear();

So a potential way to solve the memory leak on the user side would be to:

for (size_t sec = 0; sec < finalstate->GetNumberOfSecondaries(); ++sec) {
delete finalstate->GetSecondary(sec)->GetParticle();
}
finalstate->Clear();

Of course, this should be rather fixed on Geant4’s side, potentially by doing:

void ClearSecondaries() {
for(auto sec : theSecs) delete sec.GetParticle();
theSecs.clear();

}

see Prevent memleak when clearing secondaries by ferdymercury · Pull Request #95 · Geant4/geant4 · GitHub