UProceduralMeshComponent crash when mesh created outside of ctor

I am having a very strange issue when trying to get the UProceduralMeshComponent to create a mesh.
I copied this “Getting Started” guide and it worked fine.

I have the code as this:

    AProcObject::AProcObject() :
    {
    	PrimaryActorTick.bCanEverTick = true;
    
    	UProceduralMeshComponent* Mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("MeshComponent"));
    	RootComponent = Mesh;
    
    	TArray<FVector> vertices;
    	vertices.Add(FVector(0, 0, 0));
    	vertices.Add(FVector(0, 100, 0));
    	vertices.Add(FVector(0, 0, 100));
    
    	TArray<int32> Triangles;
    	Triangles.Add(0);
    	Triangles.Add(1);
    	Triangles.Add(2);
    
    	TArray<FVector> normals;
    	normals.Add(FVector(1, 0, 0));
    	normals.Add(FVector(1, 0, 0));
    	normals.Add(FVector(1, 0, 0));
    
    	TArray<FVector2D> UV0;
    	UV0.Add(FVector2D(0, 0));
    	UV0.Add(FVector2D(0, 10));
    	UV0.Add(FVector2D(10, 10));
    
    	TArray<FColor> vertexColors;
    	vertexColors.Add(FColor(100, 100, 100, 100));
    	vertexColors.Add(FColor(100, 100, 100, 100));
    	vertexColors.Add(FColor(100, 100, 100, 100));
    
    
    	TArray<FProcMeshTangent> tangents;
    	tangents.Add(FProcMeshTangent(1, 1, 1));
    	tangents.Add(FProcMeshTangent(1, 1, 1));
    	tangents.Add(FProcMeshTangent(1, 1, 1));
    
    	Mesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
}

So, the only difference is that I have the Mesh as the root object. So far so good. As soon as I move everything below the line

RootComponent = Mesh;

Into a separate function, the editor crashes! I get this error in the log

Access violation - code c0000005 (first/second chance not available)
UE4Editor_ProceduralMeshComponent!TArray<FProcMeshSection,FDefaultAllocator>::SetNum()
UE4Editor_ProceduralMeshComponent!UProceduralMeshComponent::CreateMeshSection()

So, it occurs on the call to CreateMeshSection() in the function I created… Why could this be?

Hi,

Okay, I was wrong and hasty with my first reply, here is a better one.

Looking at your error code, it is actually that you use the same index to create your procedural mesh component. Make a running index and replace the 1 in your code with it. Everytime you create a procedural mesh, it should update.

This is actually not the case, you can freely create new mesh sections over top of the others (or better yet, if you can use the UpdateMeshSection function if the triangle do not change). I solved my problem, I answered below.

Okay, well this was pretty silly of me. Late night, not enough coffee…

UProceduralMeshComponent* Mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("MeshComponent"));

Should be

Mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("MeshComponent"));

So that I am using the member variable… doh. The access violation was caused because my Mesh pointer was null!

I was indeed super wrong! Now that you said it, creating it with the same index should just replace the existing one… Duh, fairly stupid of me. Heh, but I totally missed your redeclaration too.

Curious that it didn’t give you a null pointer error.

Good job on picking it up!