Access Violation in UProceduralMeshComponent

hi people,

I am creating a procedural terrain system and I used CreateMeshSection() function, game can run once but when I run it twice from the editor it will show up this error.
I have totally no idea why…

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

UE4Editor_ProceduralMeshComponent!UProceduralMeshComponent::CreateMeshSection() [d:\build\++ue4\sync\engine\plugins\runtime\proceduralmeshcomponent\source\proceduralmeshcomponent\private\proceduralmeshcomponent.cpp:416]
UE4Editor_BlockWorld_9393!<lambda_e3db757078c41f0d22c14bc0a83d0e82>::operator()() [d:\develop\unreal\blockworld\source\blockworld\blockengine\chunkrendertask.cpp:83]
UE4Editor_BlockWorld_9393!TGraphTask<FFunctionGraphTask>::ExecuteTask() [d:\program files\epic games\ue_4.20\engine\source\runtime\core\public\async	askgraphinterfaces.h:830]

and the code in the source is:

void UProceduralMeshComponent::CreateMeshSection(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals, const TArray<FVector2D>& UV0, const TArray<FVector2D>& UV1, const TArray<FVector2D>& UV2, const TArray<FVector2D>& UV3, const TArray<FColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision)

    // Ensure sections array is long enough
    if (SectionIndex >= ProcMeshSections.Num())    // <<<< == this line
        ProcMeshSections.SetNum(SectionIndex + 1, false);

please help… thank you so so much!!

You’ve probably tried to call the method on a null/invalid pointer.

Hi, thanks for your reply.
Nope, I’ve checked by using if(!mesh){} and it’s not null.
some times it works without crashing but some times it doesn’t. The pointer is always not null.

You say you’re checking with

if (!Mesh) {}

  • which would check if the mesh IS nullptr and run the code. You need to use

if (Mesh) {}

, or to be less inconspicuous,

if (Mesh != nullptr) {}

I agree with Zeblote, it’s most likely that the mesh doesn’t exist. Use the callstack, and look at the previous entries to find the issue.