Download

Access violation when calling AddInstance

Hi there,

I am currently creating a system which automatically spawns foliage all over the map. Therefore I dynamically create a few UInstancedStaticMeshComponents in the OnConstruction method of each cluster, depending on the user parameters. I don’t use subobjectptrs because the instanced meshes are added to a TArray and the number of different instanced meshes also depends on some parameters.
However, I always get an access violation when calling the AddInstance method :frowning:

Here is a simplified code snippet which illustrates the problem:


void AFoliageCluster::OnConstruction(const FTransform& transform) 
{
      UStaticMesh* mesh = ...;   // some valid static mesh
      
      UInstancedStaticMeshComponent* newInstancedMesh = NewObject<UInstancedStaticMeshComponent>();
      newInstancedMesh->SetStaticMesh(mesh);
      // [newInstancedMesh gets added to some TArray]

      newInstancedMesh->AttachTo(RootComponent);

      FTransform renderTransform = FTransform::Identity;
      newInstancedMesh->AddInstance(renderTransform);

      // This is where the exception occurs:
      // Unhandled exception at 0x00007FF8973495F6 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000118.
}

Any ideas what the problem may be? :slight_smile:

I think I found the solution:


newInstancedMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);

When using this at least the exception goes away, but I still don’t know if it’s fully functional, need to do some more tests :smiley: