# UProceduralMeshComponent crash when mesh created outside of ctor

**URL:** <https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 7, 2016, 1:16am UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759 "2016-03-07T01:16:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S0rn0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S0rn0](https://forums.unrealengine.com/u/S0rn0)\
**Post date:** [March 7, 2016, 1:16am UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759/1 "2016-03-07T01:16:24Z")

</div>

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.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

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?

---

<div class="post-metadata">

**Author:** ![Groatse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Groatse](https://forums.unrealengine.com/u/Groatse)\
**Post date:** [March 7, 2016, 5:58pm UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759/2 "2016-03-07T17:58:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![S0rn0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S0rn0](https://forums.unrealengine.com/u/S0rn0)\
**Post date:** [March 7, 2016, 7:30pm UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759/3 "2016-03-07T19:30:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![S0rn0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S0rn0](https://forums.unrealengine.com/u/S0rn0)\
**Post date:** [March 7, 2016, 7:32pm UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759/4 "2016-03-07T19:32:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Groatse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Groatse](https://forums.unrealengine.com/u/Groatse)\
**Post date:** [March 8, 2016, 7:57pm UTC](https://forums.unrealengine.com/t/uproceduralmeshcomponent-crash-when-mesh-created-outside-of-ctor/349759/5 "2016-03-08T19:57:15Z")

</div>

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!
