Bug in TOctree2

In Math/GenericOctree.h, as part of TOctree2 there is a snippet of code in AddElementInternal that looks like this:

	TempElementStorage = MoveTemp(TreeElements[CurrentNodeIndex]);

	FNodeIndex ChildStartIndex = AllocateEightNodes();
	ParentLinks[(ChildStartIndex - 1) / 8] = CurrentNodeIndex;
	TreeNodes[CurrentNodeIndex].ChildNodes = ChildStartIndex;
	TreeNodes[CurrentNodeIndex].InclusiveNumElements = 0;

	for (typename TCallTraits<ElementType>::ConstReference ChildElement : TempElementStorage)
	{
		const FBoxCenterAndExtent ChildElementBounds(OctreeSemantics::GetBoundingBox(ChildElement));
		AddElementInternal(CurrentNodeIndex, NodeContext, ChildElementBounds, ChildElement, TempElementStorage);
	}

The key observation being that it is iterating over an array “TempElementStorage” that is being changed inside the recursive call of AddElementInternal. This is causing a crash.

I’m guessing most octree’s using this typically don’t run into an issue because OctreeSemantics::MaxElementsPerLeaf is set high enough to avoid this block of code.

What is the correct fix here? It seems like this code is used all over the engine, I’m surprised no one has run into this yet…

thank you,
jeff

Just for refrence, the semantics in the crashing case look like:

struct FBlendableOctreeSemantics
{
	enum { MaxElementsPerLeaf = 4 };
	enum { MinInclusiveElementsPerNode = 7 };
	enum { MaxNodeDepth = 32 };

	typedef TInlineAllocator<MaxElementsPerLeaf> ElementAllocator;
...