Problem 1149

Summary: ~G4SingleParticleSource() doesn't delete pointers to G4SPSPosDistribution, G4SPSAngDistribution, G4SPSEneDistribution and G4SPSRandomGenerator
Product: Geant4 Reporter: Paola <pmarini>
Component: eventAssignee: flei
Status: RESOLVED FIXED    
Severity: minor    
Priority: P5    
Version: other   
Hardware: PC   
OS: Linux   

Description Paola 2010-10-15 01:51:41 CEST
Hi,
 I've a memory leak problem.
I'm running a simulation in which I need to call the General Particle Constructor inside my GeneratePrimaries() method. I delete it every time before to create a new one, first by deleteaSource (or CleanAll) and then deleting my GeneralParticleSource. Even if I do that I ended up filling the memory of my computer. I figured out that when I call deleteaSource (or CleanAll) it is deleting the pointer to the source but not the pointers to 
G4SPSPosDistribution
G4SPSAngDistribution
G4SPSEneDistribution
G4SPSRandomGenerator
because G4SingleParticleSource::~G4SingleParticleSource() is empty.

I need to call the GeneralParticleConstructor constructor in GeneratePrimaries because I'm reading the output of a QMD simulation in which every event has a different multiplicity, so  the number of sources has to change (and that's fine. Up to here  it could be done with the GPS constructor called only once.) But what was happening was that
- if I was not deleting the sources: the number of sources was just added to the one of the previous event....and that's not what I wanted
Speculation: is not possible to reconfigure the one GeneralParticleSource constructor entirely if I want the multiplicity of events to change from one to the next event? 
- when I was trying to call ClearAll() or to loop on DeleteaSource(), my code was giving me segmentation fault  (I had the condition  if(my_GPS!=NULL) )
I have no idea of the reason.


I solved it writing a method:

void deleteArchitect(G4GeneralParticleSource* theArchitect) {
  if(theArchitect == NULL) return;
  for(G4int i=0;i<theArchitect->GetNumberofSource();i++) {
      theArchitect->SetCurrentSourceto(i);
      G4SingleParticleSource * source = 
        theArchitect->GetCurrentSource();
      G4SPSPosDistribution* del1 = source->GetPosDist();
      G4SPSAngDistribution* del2 = source->GetAngDist();
      G4SPSEneDistribution* del3 = source->GetEneDist();
      G4SPSRandomGenerator* del4 = source->GetBiasRndm();
      delete del1; delete del2; delete del3; delete del4;
      delete source;
  } 
  theArchitect->ClearAll();
  delete theArchitect;
}

but I think it could be done easier just modifying G4SingleParticleSource::~G4SingleParticleSource()  to destroy the 4 pointers

G4SingleParticleSource::~G4SingleParticleSource() {
  delete GetPosDist();
  delete GetAngDist();
  delete GetEneDist();
  delete GetBiasRndm();
}   

unless there is a reason for which it has been done like this.

Thank you very much for your help
Best regards
Paola
Comment 1 flei 2010-12-14 11:00:03 CET
Thanks for reporting the problem and the suggested modification.

This issue has been fixed now and it should come out in the new v9.4 release.

Fan