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 =
G4OpticalPhoton::OpticalPhotonDefinition();

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

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

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

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.
Backtrace:
[PID=2877, TID=8][ 0/20]> /opt/geant4/geant4-install/lib/libG4materials.so(_ZNK25G4MaterialPropertiesTable11GetPropertyEi+0xb) [0x7f275e030b7b]
[PID=2877, TID=8][ 1/20]> /opt/geant4/geant4-install/lib/libG4processes.so(_ZN19G4OpBoundaryProcess12PostStepDoItERK7G4TrackRK6G4Step+0x1d4) [0x7f275cb88244]
[PID=2877, TID=8][ 2/20]> /opt/geant4/geant4-install/lib/libG4tracking.so(_ZN17G4SteppingManager11InvokePSDIPEm+0x55) [0x7f275eaf2b15]
[PID=2877, TID=8][ 3/20]> /opt/geant4/geant4-install/lib/libG4tracking.so(_ZN17G4SteppingManager23InvokePostStepDoItProcsEv+0x8b) [0x7f275eaf32db]
[PID=2877, TID=8][ 4/20]> /opt/geant4/geant4-install/lib/libG4tracking.so(_ZN17G4SteppingManager8SteppingEv+0x46c) [0x7f275eaef81c]
[PID=2877, TID=8][ 5/20]> /opt/geant4/geant4-install/lib/libG4tracking.so(_ZN17G4TrackingManager15ProcessOneTrackEP7G4Track+0x118) [0x7f275eb05488]
[PID=2877, TID=8][ 6/20]> /opt/geant4/geant4-install/lib/libG4event.so(_ZN14G4EventManager12DoProcessingEP7G4Event+0x893) [0x7f275eb46283]
[PID=2877, TID=8][ 7/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZN22G4WorkerTaskRunManager15ProcessOneEventEi+0x3c) [0x7f275ecbdcac]
[PID=2877, TID=8][ 8/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZN22G4WorkerTaskRunManager11DoEventLoopEiPKci+0x17e) [0x7f275ecbdbee]
[PID=2877, TID=8][ 9/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZN22G4WorkerTaskRunManager6DoWorkEv+0x15d) [0x7f275ecbdf3d]
[PID=2877, TID=8][10/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZN22G4TaskRunManagerKernel17ExecuteWorkerTaskEv+0x6a) [0x7f275ecbadca]
[PID=2877, TID=8][11/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(+0x3a546) [0x7f275ecaa546]
[PID=2877, TID=8][12/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZNSt13__future_base13_State_baseV29_M_do_setEPSt8functionIFSt10unique_ptrINS_12_Result_baseENS3_8_DeleterEEvEEPb+0x2d) [0x7f275ecb26fd]
[PID=2877, TID=8][13/20]> /lib/x86_64-linux-gnu/libpthread.so.0(+0x114df) [0x7f275a8a94df]
[PID=2877, TID=8][14/20]> /opt/geant4/geant4-install/lib/libG4tasking.so(_ZN3PTL4TaskIvJEEclEv+0x12c) [0x7f275ecb287c]
[PID=2877, TID=8][15/20]> /opt/geant4/geant4-install/lib/libG4ptl.so.0(_ZN3PTL10ThreadPool14execute_threadEPNS_14VUserTaskQueueE+0x351) [0x7f275de0f6d1]
[PID=2877, TID=8][16/20]> /opt/geant4/geant4-install/lib/libG4ptl.so.0(_ZN3PTL10ThreadPool12start_threadEPS0_PSt6vectorISt10shared_ptrINS_10ThreadDataEESaIS5_EEl+0x1a3) [0x7f275de0fe33]
[PID=2877, TID=8][17/20]> /lib/x86_64-linux-gnu/libstdc++.so.6(+0xd6de4) [0x7f275dcdbde4]
[PID=2877, TID=8][18/20]> /lib/x86_64-linux-gnu/libpthread.so.0(+0x8609) [0x7f275a8a0609]
[PID=2877, TID=8][19/20]> /lib/x86_64-linux-gnu/libc.so.6(clone+0x43) [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.