New Materials Causing Segmentation Fault


I am having trouble creating new materials in Geant4. Below is an example of code causing me issues: when I try to run the code, I am met with a segmentation error. If I change the number of elements to 3 (although there are only 2 in the compound) it does not have this error immediately, but when I try to /run/beamOn it crashes with long fallback errors. I can’t identify any problems here so advice would be appreciated!

 G4double OilDensity = 0.89 * g/cm3;
    G4Material* Oil = new G4Material("Oil", OilDensity, 2);
    G4int Natoms;
    Oil->AddElement(C, Natoms = 19);
    Oil->AddElement(H, Natoms = 38);

Your material is perfectly defined (assuming that elements C and H and already well defined). The problem cannot come from here.

Both elements are defined without error with the NIST manager. The code works fine when this part is commented out - is there an error somewhere else this would usually indicate? I cannot understand why including this would crash the code

how is C and H defined?

Hi! Please see below. Thank you for your help!

G4NistManager* nist = G4NistManager::Instance();
G4Element * C = nist->FindOrBuildElement("G4_C");  
G4Element * H = nist->FindOrBuildElement("G4_H");  

this is how I think it should be:

G4Element* C  = nist->FindOrBuildElement("C");
G4Element* H = nist->FindOrBuildElement("H"); 

“G4_C” is for G4Material…

1 Like

Can you check in your code whether those “C” and “H” pointers are null? Just because you didn’t get a printed error message doesn’t me everyone is happy :slight_smile:

I think @weller identified the problem, but checking for null pointers is generally good practice.

ahh yeah it was the G4_X that was causing the problem, thank you for your help!

yeahh that makes sense, thank you for your help!

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