Adding Instance to HISMC Causes Access Violation

This occurs while playing in Standalone Game. Specifically, it occurs after an int32 is added to a TArray in HierarchicalInstancedStaticMesh.cpp on line 301, and Realloc() is called in TArray’s ResizeGrow().

I have zero idea what is causing this, or how to even begin debugging it. Please let me know what information I can provide that would help narrow down the cause of this.

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Core!rml::internal::ExtMemoryPool::initTLS()
UE4Editor_Core!rml::internal::MemoryPool::putToLLOCache()
UE4Editor_Core!scalable_realloc()
UE4Editor_Core!FMallocTBB::Realloc() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\hal\malloctbb.cpp:105]
UE4Editor_Core!FMemory::Realloc() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\hal\fmemory.inl:48]
UE4Editor_Engine!TArray<int,FDefaultAllocator>::ResizeGrow() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\containers\array.h:2244]
UE4Editor_Engine!FClusterBuilder::FClusterBuilder() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\hierarchicalinstancedstaticmesh.cpp:331]
UE4Editor_Engine!UHierarchicalInstancedStaticMeshComponent::BuildTreeAsync() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\hierarchicalinstancedstaticmesh.cpp:2473]
UE4Editor_Engine!UHierarchicalInstancedStaticMeshComponent::BuildTreeIfOutdated() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\hierarchicalinstancedstaticmesh.cpp:2425]
UE4Editor_Engine!UHierarchicalInstancedStaticMeshComponent::AddInstance() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\hierarchicalinstancedstaticmesh.cpp:2062]
UE4Editor_Engine!UInstancedStaticMeshComponent::AddInstanceWorldSpace() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\instancedstaticmesh.cpp:1582]

I believe I have figured it out! It appears to have been a race condition.

By default, a bool bAutoRebuildTreeOnInstanceChanges is set to ‘true’ in HISMCs. This causes the function BuildTreeIfOutdated() to be called after each instance add or removal:

BuildTreeIfOutdated(true, false);

It is called with the first parameter set to true, ‘Async’. This causes the cluster tree data structure to be rebuilt asynchronously.

I’m creating and destroying many HISMC instances in a Tick() function, and I believe that calling BuildTreeIfOutdated() with the Async flag causes a race condition to occur.

My solution: Set bAutoRebuildTreeOnInstanceChanges to ‘false’ when instantiating the HISMCs, and at the end of each Tick() manually call BuildTreeIfOutdated(false, false). This rebuilds the cluster tree for each HISMC once per frame after all of the adding and removing of instances occurs, and I set the Async flag to ‘false’ just for good measure.

I haven’t noticed any noticeable performance drop setting Async to ‘false’, and this solution seems to have completely fixed the problem: No access violations are occurring and everything is rendering properly.

Cheers!

That’s nice. I am making an in-editor tool using HISMCs and that fixed the crashes I was experiencing (I am using 4.20.3). Hopefully this will hold for a while.

Cheers.