Hello
We are experimenting intermittent ensures during PIE start / actor registration when a level contains a UGeometryCacheComponent:
ensureMsgf(CurrentNum == InitialNum, TEXT(“Array has changed during ranged-for iteration!”))
Callstack is on the game thread (or a parallel-game-thread registration task) inside
FGeometryCacheSceneProxy::CreateHitProxies, iterating Track->MeshData->BatchesInfo.
Root cause
1. FGeometryCacheSceneProxy::FGeometryCacheSceneProxy (GeometryCacheSceneProxy.cpp ~L162-180) decodes the
current frame into NewSection->MeshData on the creating thread, then immediately enqueues
ENQUEUE_RENDER_COMMAND(FGeometryCacheUpdateAnimation) { SceneProxy->FrameUpdate(RHICmdList); }.
2. After CreateSceneProxy() returns, FScene::BatchAddPrimitivesInternal constructs FPrimitiveSceneInfo,
which (GIsEditor) calls Proxy->CreateHitProxies (PrimitiveSceneInfo.cpp ~L240). That function
ranged-fors over Track->MeshData->BatchesInfo (GeometryCacheSceneProxy.cpp ~L354).
3. The render thread may already be executing the FrameUpdate from step 1. For a new track proxy
FrameIndex == NextFrameIndex == -1, so FrameUpdate takes the “seek” branch
(GeometryCacheSceneProxy.cpp ~L734-736):
if (TrackProxy->GetMeshData(FrameIndex, *TrackProxy->MeshData))
which ends in FCodecV1Decoder::DecodeFrameData (CodecV1.cpp ~L1110):
Reader << OutMeshData.BatchesInfo;
FGeometryCacheMeshBatchInfo has a custom operator<<, so TArray loading uses the per-item path:
A.Empty(SerializeNum) followed by AddDefaulted_GetRef per element. Num transiently goes to 0 and
back up while the other thread is iterating the same array, and the ranged-for check fires.
The final count is usually identical, which is why it is intermittent.
Suggested fix
Do not hand the proxy to the render thread from the constructor. Move the two constructor enqueues
(FGeometryCacheUpdateAnimation / FrameUpdate and FGeometryCacheInitRayTracingGeometry) into an
override of FPrimitiveSceneProxy::CreateRenderThreadResources, which runs inside the scene’s
AddPrimitiveCommand, i.e. after the FPrimitiveSceneInfo and its hit proxies exist.
Thanks
[Attachment Removed]