Should G4TouchableHandle be used as a data member?

Geant4 Version: 10.07.p04, 11.4.0
Operating System: MacOS, Linux
Compiler/Version: GCC, Apple Clang
CMake Version: 3.28, 4.2.1


TL;DR: We have G4TouchableHandle data member in a class which appears to cause a per track/per event memory leak.

We have implemented a local G4ParticleChange subclass in the G4CMP phonon transport library, which allows us to modify the volume that is going to be assigned to the post-step for reasons (I can point John A. at relevant slide decks).

To do this, our ParticleChange subclass includes a G4TouchableHandle data member, which can be filled via our own ProposeTouchableHandle(const G4TouchableHandle&) mutator.

  • In Initialize(), we set it equal to the track’s own current TouchableHandle, and we have a flag we use to know if it’s been changed or not.
  • When UpdateStepForPostStep() gets called, we check that flag, if if the handle was changed, we update the PostStepPoint’s LV, Handle, Material, MaterialCutsCouple, and SD.
  • Then in UpdateStepForPostStep(), we explicitly set theTouchableHandle = 0; to clear it before the next cycle.

When we run using this code, we are seeing a massive increase in memory, which I don’t understand. Is what we’re doing above sensible? Can G4TouchableHandle be a data member that gets reassigned during the job? If it can, should it be reset using "= 0, or is there something else to do?

The “=” operator is overloaded, try using “delete” instead.

Right! I can’t use delete because the data member is an object, not a pointer. I was relying on the overloaded operator=(), passing in a null pointer to get an “empty” Handle (i.e., one with fCount=0).