Runtime exception during execution, leading to a crash

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version: 11.1
_Operating System: Windows 10
_Compiler/Version: Microsoft Visual Studio Community 2022 (64-bit) - Version 17.9.0
_CMake Version: 3.25.1

Hello everybody,

Could anyone help me identify the following error? (Please see the attached snapshots.)

I am attempting to simulate the mass attenuation coefficient for various materials, such as alloys, glass, etc. No compilation issues were raised (see attached photo 1) . However, a runtime exception occurred during execution, leading to a crash (see the attached photo 2).

Please note that I can run the B1 example without any issues.

Is there any advice? Thank in advance


The error message is telling you that a nullptr was passed to some call to G4ParticleGun::SetParticleDefinition(), so check for calls to this in your code and see how/why a nullptr is getting passed.

Thank you @bmorgan for your answer. I didn’t use G4ParticleGun::SetParticleDefinition() in my code. Maybe this is my mistake. I use the following function in my PrimaryGenerator.cc. Let me play with it and I will update you with my results.

PrimaryGeneratorAction::PrimaryGeneratorAction() {
    // Create particle gun
    particleGun = new G4ParticleGun(1);

    // Get particle table
    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
    G4String particleName;
    G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "gamma");
    particleGun->SetParticleDefinition(particle);

####################################################

My PrimaryGeneratorAction.cc file is:

#include "PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4SystemOfUnits.hh"

PrimaryGeneratorAction::PrimaryGeneratorAction() {
    // Create particle gun
    particleGun = new G4ParticleGun(1);

    // Get particle table
    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
    G4String particleName;
    G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "gamma");
    particleGun->SetParticleDefinition(particle);
}

PrimaryGeneratorAction::~PrimaryGeneratorAction() {
    delete particleGun;
}

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
    // Set array of photon energies
    G4double photonEnergies[] = { 0.015, 0.1, 0.5, 1, 3, 5, 10, 15 }; // in MeV

    // Loop through each photon energy and generate primaries
    for (size_t i = 0; i < sizeof(photonEnergies) / sizeof(photonEnergies[0]); ++i) {
        G4double energy = photonEnergies[i] * MeV;

        // Set particle energy
        particleGun->SetParticleEnergy(energy);

        // Set particle position
        G4double positionX = 0.0;
        G4double positionY = 0.0;
        G4double positionZ = -5.0 * cm; // Position just above the source
        particleGun->SetParticlePosition(G4ThreeVector(positionX, positionY, positionZ));

        // Set particle direction
        G4double directionX = 0.0;
        G4double directionY = 0.0;
        G4double directionZ = 1.0;
        particleGun->SetParticleMomentumDirection(G4ThreeVector(directionX, directionY, directionZ));

        // Generate primary particle
        particleGun->GeneratePrimaryVertex(anEvent);
    }
}

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