Segmentation fault when storing hits using sensitive detectors

I have been doing simulations that involve scintillating panels, each panel has two detectors, one on each end. I am wanting to store the photons that hit each detector. I was able to collect the data for one panel and data for a two panel setup without any issue, but now that I am doing an eight panel setup I am getting segmentation faults. For smaller runs, the segmentation faults are inconsistent and I sometimes do not get them, but they always occur for larger runs (more decays).

I believe it has something to do with storing the hits per detector because if I just store the positions of the photons when they hit I do not get the error, but when I try to store the hits of the detectors separately I get this error. I have been running in parallel but the error is still there even when thread number is set to 1. Bellow is the error message, the backtrace, and some segments of the code that I think are causing the issue. If more information is needed please let me know, I am trying not to be overwhelming with chunks of code.

Error Message

Thread 10 "sim" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe92f7700 (LWP 8151)]
0x00007ffff6d1f77c in G4ReferenceCountedHandle<G4TouchableHistory>::operator=(G4ReferenceCountedHandle<G4TouchableHistory> const&) [clone .isra.0] () from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so

Backtrace

#0  0x00007ffff6d1f77c in G4ReferenceCountedHandle<G4TouchableHistory>::operator=(G4ReferenceCountedHandle<G4TouchableHistory> const&) [clone .isra.0] () from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#1  0x00007ffff6d20caa in G4Step::UpdateTrack() () from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#2  0x00007ffff6d2014e in G4SteppingManager::InvokePSDIP(unsigned long) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#3  0x00007ffff6d20260 in G4SteppingManager::InvokePostStepDoItProcs() ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#4  0x00007ffff6d204c8 in G4SteppingManager::Stepping() ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#5  0x00007ffff6d2a5e3 in G4TrackingManager::ProcessOneTrack(G4Track*) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so
#6  0x00007ffff6d62165 in G4EventManager::DoProcessing(G4Event*) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4event.so
#7  0x00007ffff6e167af in G4WorkerRunManager::ProcessOneEvent(int) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4run.so
#8  0x00007ffff6e17234 in G4WorkerRunManager::DoEventLoop(int, char const*, int) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4run.so
#9  0x00007ffff6df2894 in G4RunManager::BeamOn(int, char const*, int) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4run.so
#10 0x00007ffff6e16ca2 in G4WorkerRunManager::DoWork() () from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4run.so
#11 0x00007ffff6dede37 in G4MTRunManagerKernel::StartThread(G4WorkerThread*) ()
   from /home/seth/geant4/geant4-v11.2.1-install/lib/libG4run.so
#12 0x00007ffff343fdf4 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007ffff31e2609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#14 0x00007ffff3107353 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Code from run.cc

MyRunAction::MyRunAction()
{
  G4AnalysisManager* man = G4AnalysisManager::Instance();

 // Code here for storing energy deposited into scintillator
// No segmentation fault when this is the only thing here

  for (G4int i = 1; i <= 16; i++) {   //Causing Issue when included
    auto ntupleName = "Detector " + std::to_string(i);
    man->CreateNtuple(ntupleName, ntupleName);
    man->CreateNtupleIColumn("fEvent");
    man->FinishNtuple(i + 2);
  }
}

Code from detector.cc

G4bool MySensitiveDetector::ProcessHits(G4Step* aStep, G4TouchableHistory* ROhist)
{
  G4Track* track = aStep->GetTrack();

  track->SetTrackStatus(fStopAndKill);

  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();

//code related to other information stored 

  const G4VTouchable* touchable = aStep->GetPreStepPoint()->GetTouchable();
  G4int copyNo = touchable->GetCopyNumber();  // The detector number/identity.

  G4VPhysicalVolume* physVol = touchable->GetVolume();
  G4ThreeVector posDetector = physVol->GetTranslation();  // Position of the detector.

  G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();

  G4AnalysisManager* man = G4AnalysisManager::Instance();
  
//Storing info about energy deposited and such, also was not having issue when 
//this was the only thing here

  for (G4int i = 0; i < 16; i++) {  // Causing issue when included
    if (copyNo == i) {
      man->FillNtupleIColumn(i + 3, 0, evt);
      man->AddNtupleRow(i + 3);
    }
  }
  return true;
}

_Geant4 Version:_geant4-v11.2.1
_Operating System:_WSL 2 on windows 11
Compiler/Version:
_CMake Version:_cmake version 3.16.3


You have spaces in the tuple name.

1 Like

I got rid of the space in the tuple and I’m still having the issue.
The error I am getting says that SEGV on unknown address 0xffffffffffffffe0 and the seg signal was sent be a WRITE memory access, and the backtrace says it happens in G4ReferenceCountedHandle<G4TouchableHistory>::operator=(G4ReferenceCountedHandle<G4TouchableHistory> const&) [clone .isra.0] (/home/seth/geant4/geant4-v11.2.1-install/lib/libG4tracking.so+0x2177b)

in my detector.cc I do the following in relation to the touchable history

G4bool MySensitiveDetector::ProcessHits(G4Step* aStep, G4TouchableHistory* ROhist)
{
  G4Track* track = aStep->GetTrack();

  track->SetTrackStatus(fStopAndKill);

  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();

  const G4VTouchable* touchable = aStep->GetPreStepPoint()->GetTouchable();
  G4int copyNo = touchable->GetCopyNumber();  // The detector number/identity.

It seems that the pointer for the touchable history is passing a max value for some reason and it is having trouble dereferencing it.

The address suggests that the initial reference is null. preStepPoint or postStepPoint may be a nullptr.

I checked that, I added

  if (!preStepPoint) std::cout << "\nPrestep point was null pointer!\n";
  if (!postStepPoint) std::cout << "\nPoststep point was null pointer!\n";

  const G4VTouchable* touchable = preStepPoint->GetTouchable();

  if (!touchable) std::cout << "\nTouchable pointer was null pointer!\n";

and re-ran the simulation. I am still getting the segfault but have none of the print statements suggesting that prestep, poststep or touchable were nullptr, unless you’re suggesting they’re nullptr somewhere else

Earlier I was printing out these three for every step just to see what they looked like before the crash, and they appeared to be all normal addresses.

std::cout prints to the terminal from which your app was launched. Have you looked there? Use G4cout to get output in the UI window.

1 Like

I was using std::count and looking in the Ubuntu window where I ran the code, I typically run it without the GUI. If it was detecting nullptr I should’ve seen it, I tested it before hand with a variable I explicitly defined as nullptr to make sure the comparison operator and print statement would work correctly