Memory leak looping

_Geant4 Version: geant4-v10.7.4
_Operating System: MacOS 13.6.4 (22G513)
_Compiler/Version: GNU Make 3.81
_CMake Version: 3.28.0-rc5

Starting several run in a loop (see code portion below), I get a memory leak and the process killed at a certain point by the OS. I need to re-initilize the geometry at each run, and I assume that the old one is kept into memory. For this the leak. Am I wrong? How could solve this (or the possible source of mistake you may spot)

Thanks,
David

below how I loop

for(int i = first_step; i < primPos; i++){
DataManager* man = new DataManager;
G4int primStep = 5*i; //each 5 mm for 44 position = 220 mm
if (i==first_step)
runManager->SetUserInitialization(new PhysicsList);

for(int j=0; j<detPos; j++){
  int position = j; // 8 for test, target center
  snprintf(name,100,"G4SHADES_gasTarget_extended_primPos%i_detPos%i.root",(110-primStep),position);

// snprintf(name,100,“test”);
runManager->SetUserInitialization(new DetectorConstruction(man,position)); // 0->14 complete scan
runManager->SetUserAction(new PrimaryGeneratorAction((double)primStep, man)); // 0->220 full target length
runManager->SetUserAction(new RunAction(man, name));
runManager->SetUserAction(new EventAction(man));
runManager->Initialize();
runManager->ReinitializeGeometry();
runManager->BeamOn(1000000);
}

delete man;

}

Dear @David_Rapagnani

@dkonst and I found this issue last year, it is likely related to a leak of the voxelization map (which is not deleted after each reinitialize call) but I have not tried to disable voxelization to test this idea.

In the meantime, you may try to disable voxelization (but simulation time may be increased by a lot), or simply launch your application for each geometry so there is no need to reinitialize it for each run.

I will let you know if I manage to fix it before the next release.

Thank you for reporting!

Best,
Alvaro