Unique_ptr Segfault

I am trying to update the following to g4.11.2.0 from 11.1

class ZStepLimiter : public G4VProcess {
	static G4double maxStep;
	class ParticleChange : public G4VParticleChange {
	public:
		ParticleChange() : G4VParticleChange() { }
	};
  std::unique_ptr<ParticleChange> change = std::make_unique<ParticleChange>();
public:
	ZStepLimiter() : G4VProcess("ZStepLimiter",fUserDefined), change() {
	    G4ParticleTable::G4PTblDicIterator *myParticleIterator =
			G4ParticleTable::GetParticleTable()->GetIterator();
	    myParticleIterator->reset();
	    while((*myParticleIterator)()) {
		G4ParticleDefinition *pd = myParticleIterator->value();
		G4ProcessManager *pmgr = pd->GetProcessManager();
		if(!pmgr) {
			printf("ZStepLimiter: particle '%s' has no ProcessManager!\n",
				pd->GetParticleName().c_str());
			continue;
		}
		pmgr->AddProcess(this,-1,-1,ordDefault);
	    }
	}

	G4VProcess *clone() { return new ZStepLimiter(); }

However following converting the class to a unique pointer, i get the following segfault on this line subsequently.

	G4VParticleChange* PostStepDoIt( const G4Track& track, 
      			const G4Step&  stepData)
  { (*change).Initialize(track);
    return change.operator->();
		}


Any help into what is causing this error would be greatly appreciated.
Kind Regards
_Geant4 Version:_11.2
_Operating System:_Debian12
_Compiler/Version:_12.2
_CMake Version:_3.25

What’s the actual report from the backtrace? Isn’t line 114 returning (what will be) a dangling pointer?

I need to rerun when im back at my pc but it points to other parts of the code not line 114. I will get back to you with the backtrace.

─── Stack ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[0] from 0x0000555555b10c26 in ZStepLimiter::PostStepDoIt(G4Track const&, G4Step const&)+38 at /home//G4//g4bl/BLManager.cc:113
[1] from 0x0000555555e3eb96 in G4SteppingManager::InvokePSDIP(unsigned long)+54 at /home//GEANT4-11/geant4/source/tracking/src/G4SteppingManager.cc:818
[2] from 0x0000555555e3ec37 in G4SteppingManager::InvokePostStepDoItProcs()+41 at /home//GEANT4-11/geant4/source/tracking/src/G4SteppingManager.cc:790
[3] from 0x0000555555e3efc1 in G4SteppingManager::Stepping()+705 at /home//GEANT4-11/geant4/source/tracking/src/G4SteppingManager.cc:225
[4] from 0x0000555555e39810 in G4TrackingManager::ProcessOneTrack(G4Track*)+428 at /home//GEANT4-11/geant4/source/tracking/src/G4TrackingManager.cc:133
[5] from 0x0000555555e0c12e in G4EventManager::DoProcessing(G4Event*)+1080 at /home//GEANT4-11/geant4/source/event/src/G4EventManager.cc:211
[6] from 0x0000555555e0c534 in G4EventManager::ProcessOneEvent(G4Event*)+16 at /home//GEANT4-11/geant4/source/event/src/G4EventManager.cc:447
[7] from 0x0000555555b29fe0 in BLRunManager::DoEventLoop(int, char const*, int)+270 at /home//G4//g4bl/BLRunManager.cc:166
[8] from 0x0000555555de63de in G4RunManager::BeamOn(int, char const*, int)+94 at /home//GEANT4-11/geant4/source/run/src/G4RunManager.cc:261
[9] from 0x0000555555b29ece in BLRunManager::BeamOn(int, char const*, int)+112 at /home//G4//g4bl/BLRunManager.cc:142
[+]

this is the backtrace

TBH, I’m really not sure what’s going on, so all I can suggest is checking for the usual things like the unique_ptr holding a non-null pointer, of if there’s the possibility of it ever holding a dangling pointer. You might need to set appropriate breakpoints and then step and examine values to see whether it’s in the unique_ptr or in Initialize.

I think the global answer goes back to this post you answered a while back.

ie relating to G4VParticleChange() having been marked as delete in 11.2 and how one is supposed to use these classes from now on.
If there is a resource about this that you could point me to I would appreciate it, as I think it is related to all other applications that one may need to run with up to date GEANT and associated physics lists.
I can’t see anything relating to G4VProcess or G4VParticleChange here:
https://geant4.web.cern.ch/download/release-notes/notes-v11.2.0.html

I should note in this line:
G4VProcess *clone() { return new ZStepLimiter(); I deleted the *this from ZStepLimiter(*this) as it was previously as it wont compile otherwise.

I must say I cant discern a fundamental difference here from 11.1 to 11.2
https://geant4.kek.jp/lxr/source/track/src/G4VParticleChange.cc

Thanks for your help and input!
Much appreciated.

Moving to Physics category as this is really about implementing custom processes. Maybe @civanch can answer this?

1 Like

Hi,
Did you find any solutions?

Unfortunately no. Perhaps @civanch can help?