Unexpected behaviour relating to thread safety

Geant4 Version: 11.2.1 and 11.1.1
Operating System: Ubuntu 22.04.4 LTS (jammy)
Compiler/Version: g++ (conda-forge gcc 10.3.0-16) 10.3.0
CMake Version: cmake version 3.26.4

I have encountered segmentation faults in my SteppingAction caused by lines like track->GetNextVolume()->GetName(). In gdb it looked like something lower level relating to how strings are handled. I have always made sure to avoid de-referencing nullpointers with if ( pointer ) {...}. So the case in which next volume is “OutOfWorld” is handled properly.

Since this issue

  1. exclusively occurred in multithread-mode and
  2. its occurrence seemed somehow random (I could not reproduce it for fixed seeds)

it looked like some race-condition which is strange as I assumed the SteppingAction to be instantiated on each thread.

What fixed the issue for now was to avoid declaring G4Track/G4StepPoint/G4VPhysicalVolume-Variables in the header but only declare them within the scope of ::UserSteppingAction. I am writing this in part to have this behavior documented and also I would like to know:

Is this expected behavior? Or am I just using Geant4 or C++ wrongly? I used very similar code before and never had issues with such segfaults. I can provide more of my code upon request.