Bad_alloc in G4RegularNavigation

I am simulating a voxel model using the G4PhantomParameterisation and run into bad_alloc on a small fraction of events (approx. 1e-8). I was able to track down the bug to the G4RegularNavigation::ComputeStepSkippingEqualMaterials getting stuck in the for(;:wink: loop and appending entries to G4RegularNavigationHelper::Instance()->AddStepLength( copyNo, newStep ) until all available memory is occupied.
This happens, when the track is moving exactly along an edge of a voxel, such that voxelBox->DistanceToOut( localPoint, localDirection ) returns 0. In this cases, the length is only increased by kCarTolerance and since every iteration of the for loop creates an entry in the G4RegularNavigationHelper::theStepLengths vector, it might happen, that all memory is allocated, before the next voxel with a different material is reached.

This can be reproduced using localPosition = (-0.5,-0.154576,0.5), localDirection = (1,0,6.12323e-17) and a voxel halfsize of (0.5,0.5,0.5).

I see, that this problem occurrs due to floating point precision issues.
I propose to change the function G4RegularNavigation::ComputeStepSkippingEqualMaterials such, that an additional check is added, to see if the copyNo has changed (which is assumed implicitly at the moment) and sum up all lengths in the same copyNo before adding it to the G4RegularNavigationHelper::theStepLengths vector.

Is there any workaround until this issue is resolved, e.g. recovering the run after one event failed?

Hi, can you please open a ticket in the Geant4 Bugzilla system giving all the details of the problem? Thanks.