How to determine whether a particle penetrates a volume?

Here is my code

   G4LogicalVolume *pre_volume
                = step->GetPreStepPoint()->GetTouchableHandle()
        G4VPhysicalVolume *post_phy_volume
                = step->GetPostStepPoint()->GetTouchableHandle()
        G4LogicalVolume *post_volume
                = post_phy_volume

        if (post_phy_volume){
       if (post_volume==fScoringVolume1){  G4cout<<" Penetrate"<<G4endl;}

But when I run ,the pprogram crashed.
If I delete

   if (post_volume==fScoringVolume1){  G4cout<<" Penetrate"<<G4endl;}

It won’t.
I wanna why

Because there is no could be volume in PostStepPoint at the end of modeling steps so you addressing to zero pointer and want to getlLogicalVolume.

But the if (post_phy_volume) should make sure that the PhysicalVolume of PostStepPiont is not a nullptr.