Segmentation fault in Stepping Action Class

This is my StepingAction Class:

#include "MySteppingAction.hh"
#include "MyEventAction.hh" 
#include "G4Step.hh"
#include "G4TouchableHistory.hh"
#include "G4Track.hh"
#include "G4ParticleDefinition.hh"
#include "G4IonTable.hh"
#include "G4ParticleTypes.hh"  
#include <iostream>
MySteppingAction::MySteppingAction(MyEventAction* eventAction) : eventAction(eventAction) {}

MySteppingAction::~MySteppingAction(){}
void MySteppingAction::UserSteppingAction(const G4Step* step) {
	G4Track* track = step->GetTrack();
	G4ParticleDefinition* particle = track->GetDefinition();

	G4VPhysicalVolume* nextVol =  track->GetNextVolume();
	G4VPhysicalVolume* Vol =  track->GetVolume();

	std::cout << Vol->GetName() << "\t" << nextVol->GetName() << std::endl;

	// Check if the particle is an ion
	if (particle->GetParticleType() != "nucleus") {
		// Kill the track if it is not an ion
		track->SetTrackStatus(fStopAndKill);
	}

}

And this code is working fine if I comment this line of above code:

std::cout << Vol->GetName() << “\t” << nextVol->GetName() << std::endl;

And if I uncomment this, it throws segmentation fault like shown in the attached figure.

test

May someone tell me what is problem with code here?

When the track leaves the world, there is no “next volume,” so GetNextVolume() returns a null pointer. It is your responsibility, when writing your code, to check for null pointers before dereferencing them.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.