Segment Fault when simulate optical photon

Hi,when i want to simulate a ZnS scintillator and emit just 5000 e-,the fault happens
I don’t know what the problem is . If the number of primary e- decrease to 500,for example,the fault didn’t occur.
Here is my mainly work,to count the optical photon through the interface,
void SteppingAction::UserSteppingAction(const G4Step* step)
static G4ParticleDefinition* opticalphoton =

    if (!fScoringVolume1||!fScoringVolume2) {
        const DetectorConstruction* detConstruction
                = static_cast<const DetectorConstruction*>
        fScoringVolume1 = detConstruction->GetScoringVolume1();
        fScoringVolume2 = detConstruction->GetScoringVolume2();

    // get volume of the current step
    G4LogicalVolume* prevolume
            = step->GetPreStepPoint()->GetTouchableHandle()

    G4LogicalVolume* postvolume
            = step->GetPostStepPoint()->GetTouchableHandle()
    if( prevolume != fScoringVolume1||postvolume!=fScoringVolume2||step->GetTrack()->GetDynamicParticle()->GetParticleDefinition()!=opticalphoton) return;

Do i need to kill the optical photon in the fScoringVolume2?

A common mistake is that, when a step point is at the world boundary, one of these pointers is null. Try checking for nullptr prior to dereferencing.

Aside from that, please run in debug mode and show us the backtrace.

But,it seems not that question.I annotated this code,the fault still ouccured.
Here’s the backtrace.
[PID=2877, TID=8][ 0/20]> /opt/geant4/geant4-install/lib/ [0x7f275e030b7b]
[PID=2877, TID=8][ 1/20]> /opt/geant4/geant4-install/lib/ [0x7f275cb88244]
[PID=2877, TID=8][ 2/20]> /opt/geant4/geant4-install/lib/ [0x7f275eaf2b15]
[PID=2877, TID=8][ 3/20]> /opt/geant4/geant4-install/lib/ [0x7f275eaf32db]
[PID=2877, TID=8][ 4/20]> /opt/geant4/geant4-install/lib/ [0x7f275eaef81c]
[PID=2877, TID=8][ 5/20]> /opt/geant4/geant4-install/lib/ [0x7f275eb05488]
[PID=2877, TID=8][ 6/20]> /opt/geant4/geant4-install/lib/ [0x7f275eb46283]
[PID=2877, TID=8][ 7/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecbdcac]
[PID=2877, TID=8][ 8/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecbdbee]
[PID=2877, TID=8][ 9/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecbdf3d]
[PID=2877, TID=8][10/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecbadca]
[PID=2877, TID=8][11/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecaa546]
[PID=2877, TID=8][12/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecb26fd]
[PID=2877, TID=8][13/20]> /lib/x86_64-linux-gnu/ [0x7f275a8a94df]
[PID=2877, TID=8][14/20]> /opt/geant4/geant4-install/lib/ [0x7f275ecb287c]
[PID=2877, TID=8][15/20]> /opt/geant4/geant4-install/lib/ [0x7f275de0f6d1]
[PID=2877, TID=8][16/20]> /opt/geant4/geant4-install/lib/ [0x7f275de0fe33]
[PID=2877, TID=8][17/20]> /lib/x86_64-linux-gnu/ [0x7f275dcdbde4]
[PID=2877, TID=8][18/20]> /lib/x86_64-linux-gnu/ [0x7f275a8a0609]
[PID=2877, TID=8][19/20]> /lib/x86_64-linux-gnu/ [0x7f275d9c8133]

: Segmentation fault (Address not mapped to object [0x8])

It’s a start. We can see from the top of the backtrace that the crash occurs when G4OpBoundaryProcess tries to get a material property, by calling GetProperty. Now we need to find out exactly the problem. If you compile with debug enabled, we can see exactly where the problem is.

Create a new Geant4 installation (leave the existing one unchanged), and run cmake with the flag -DCMAKE_BUILD_TYPE=RelWithDebInfo (or Debug); make and make install; run the your program with a debugger like gdb. When the program crashes you can get the line number and variable values.

Another thing to try is call DumpTable on your MaterialPropertiesTable (see the optical examples, maybe OpNovice2) and see that it looks correct.

Post if you’d like these comments fleshed out.