My UE4 project crashed when I try to put my created class into viewport

Below is report of (UE4 crash reporter)

Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000300

0x0000000008A96F5C UE4Editor-Engine.dll!UnknownFunction []
0x000000000891B106 UE4Editor-Engine.dll!UnknownFunction []
0x0000000008941C29 UE4Editor-Engine.dll!UnknownFunction []
0x0000000008940F15 UE4Editor-Engine.dll!UnknownFunction []
0x0000000032A54352 UE4Editor-TestProject0-928-Win64-DebugGame.dll!ASnake::AddSplineMeshComponents() [c:\users\documents\unreal projects\testproject0\source\testproject0\snake.cpp:65]
0x0000000032A54531 UE4Editor-TestProject0-928-Win64-DebugGame.dll!ASnake::OnConstruction() [c:\users\documents\unreal projects\testproject0\source\testproject0\snake.cpp:41]
0x0000000008504F1C UE4Editor-Engine.dll!UnknownFunction []
0x0000000008509F75 UE4Editor-Engine.dll!UnknownFunction []
0x000000000852DC8E UE4Editor-Engine.dll!UnknownFunction []
0x0000000008DD6787 UE4Editor-Engine.dll!UnknownFunction []
0x0000000008DD6B67 UE4Editor-Engine.dll!UnknownFunction []
0x00000000134F6D6B UE4Editor-UnrealEd.dll!UnknownFunction []
0x00000000139E4FC4 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00000000139C1262 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00000000139C0BED UE4Editor-UnrealEd.dll!UnknownFunction []
0x00000000037C6EA4 UE4Editor-LevelEditor.dll!UnknownFunction []
0x00000000037E10DE UE4Editor-LevelEditor.dll!UnknownFunction []
0x00000000150A7B93 UE4Editor-Slate.dll!UnknownFunction []
0x0000000015076A0B UE4Editor-Slate.dll!UnknownFunction []
0x0000000015111CCD UE4Editor-Slate.dll!UnknownFunction []
0x00000000150FBA28 UE4Editor-Slate.dll!UnknownFunction []
0x00000000150ED6B7 UE4Editor-Slate.dll!UnknownFunction []
0x00000000359CE39E UE4Editor-ApplicationCore.dll!UnknownFunction []
0x00000000359C1A48 UE4Editor-ApplicationCore.dll!UnknownFunction []
0x00000000359D03EC UE4Editor-ApplicationCore.dll!UnknownFunction []
0x00000000359BD5A9 UE4Editor-ApplicationCore.dll!UnknownFunction []
0x000000004808B85D USER32.dll!UnknownFunction []
0x000000004808B1EF USER32.dll!UnknownFunction []
0x00000000359D1516 UE4Editor-ApplicationCore.dll!UnknownFunction []
0x00000000751250C8 UE4Editor.exe!UnknownFunction []
0x0000000075135430 UE4Editor.exe!UnknownFunction []
0x00000000751354AA UE4Editor.exe!UnknownFunction []
0x0000000075142379 UE4Editor.exe!UnknownFunction []
0x0000000075143D57 UE4Editor.exe!UnknownFunction []
0x000000004A1C1FE4 KERNEL32.DLL!UnknownFunction []
0x000000004A72EFB1 ntdll.dll!UnknownFunction []
0x000000004A72EFB1 ntdll.dll!UnknownFunction []

My Snake.h file below is it.
#pragma once
#include “EngineGlobals.h”
#include “Components/SplineComponent.h”
#include “Components/SplineMeshComponent.h”
#include “Engine/GameEngine.h”
#include “Engine/StaticMesh.h”
#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “Snake.generated.h”

UCLASS()
class TESTPROJECT0_API ASnake : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASnake();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	// Colled when the ue4 one
	virtual void OnConstruction(const FTransform &Transform) override;
public:
	
	// Root Scene Component
	UPROPERTY(EditAnywhere, Category = "RootComponent")
		USceneComponent * SceneComponent;

	// Spline Component
	UPROPERTY(EditAnywhere, Category = "SplineComponent")
		USplineComponent * SplineComponent;

	// Static Mesh
	UPROPERTY(EditAnywhere, Category = "Static Mesh")
		UStaticMesh * StaticMesh;

	TArray<USplineMeshComponent *> SplineMeshComponentArray;

	TEnumAsByte<ESplineMeshAxis::Type> InForwardAxis;

private:
	// Add Spline Mesh Component
	void AddSplineMeshComponents();

	//  SEt Spline Mesh Component Start and End location
	void SetSplineMeshComponentStartAndEndLocation();

	// PrintString
	void PrintString(const FString &String);

};

My Snake.cpp file below is it.

   #include "Snake.h"
    
    
    // Sets default values
    ASnake::ASnake()
    {
     	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    	PrimaryActorTick.bCanEverTick = true;
    	SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    	check(SceneComponent != nullptr && "The SceneComponent is null");
    	RootComponent = SceneComponent;
    	SplineComponent = CreateDefaultSubobject<USplineComponent>(TEXT("SplineComponent"));
    	check(SplineComponent != nullptr && "The SplineComponent is null.");
    	SplineComponent->SetupAttachment(RootComponent);
    	StaticMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Static Mesh"));
    	check(StaticMesh != nullptr && "The StaticMesh is null.");
    	InForwardAxis = ESplineMeshAxis::Y;
    }
    
    // Called when the game starts or when spawned
    void ASnake::BeginPlay()
    {
    	Super::BeginPlay();
    	
    }
    
    // Called every frame
    void ASnake::Tick(float DeltaTime)
    {
    	Super::Tick(DeltaTime);
    
    }
    
    void ASnake::OnConstruction(const FTransform & Transform)
    {
    	Super::OnConstruction(Transform);
    	PrintString("OnConstruction");
    	AddSplineMeshComponents();
    	SetSplineMeshComponentStartAndEndLocation();
    }
    
    void ASnake::AddSplineMeshComponents()
    {
    	SplineMeshComponentArray.Empty();
    	const int32 Count = SplineComponent->GetNumberOfSplinePoints()-1;
    	PrintString(FString::Printf(TEXT("Count = %d"), Count));
    	UE_LOG(LogTemp, Warning, TEXT("Count = %d"), Count);
    	for (int32 i = 0; i < Count; i++)
    	{
    		FName Name = *FString::Printf(TEXT("SplineMeshComponent%d"), i);
    		USplineMeshComponent * SplineMeshComponent = NewObject<USplineMeshComponent>(this, Name);
    		if (IsValid(SplineMeshComponent) && IsValid(StaticMesh))
    		{
    			SplineMeshComponent->SetupAttachment(SplineComponent);
    			SplineMeshComponent->SetSimulatePhysics(true);
    			SplineMeshComponent->SetCollisionProfileName(TEXT("BlockAllDynamic"));
    			SplineMeshComponent->SetMobility(EComponentMobility::Movable);
    			SplineMeshComponent->SetStaticMesh(StaticMesh);
    			SplineMeshComponent->SetForwardAxis(InForwardAxis);
    			SplineMeshComponentArray.Add(SplineMeshComponent);
    			SplineMeshComponent->RegisterComponent();
    		}
    	}
    }
    
    void ASnake::SetSplineMeshComponentStartAndEndLocation()
    {
    	const uint32 Count = SplineMeshComponentArray.Num();
    	for (uint32 i = 0; i < Count; i++)
    	{
    		FVector StartLocation;
    		FVector StartTangent;
    		FVector EndLocation;
    		FVector EndTangent;
    		SplineComponent->GetLocationAndTangentAtSplinePoint(i, StartLocation, StartTangent, ESplineCoordinateSpace::Local);
    		SplineComponent->GetLocationAndTangentAtSplinePoint(i+1, EndLocation, EndTangent, ESplineCoordinateSpace::Local);
    		SplineMeshComponentArray[i]->SetStartAndEnd(StartLocation, StartTangent, EndLocation, EndTangent, true);
    	}
    }
    
    void ASnake::PrintString(const FString & String)
    {
    	if (GEngine)
    	{
    		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("%s"), *String));
    	}
    }

Crashed in the 50 line

void ASnake::AddSplineMeshComponents()
{
	SplineMeshComponentArray.Empty();
	const int32 Count = SplineComponent->GetNumberOfSplinePoints()-1;
	PrintString(FString::Printf(TEXT("Count = %d"), Count));
49.	UE_LOG(LogTemp, Warning, TEXT("Count = %d"), Count);
50.	for (int32 i = 0; i < Count; i++)
51.	{
52.		FName Name = *FString::Printf(TEXT("SplineMeshComponent%d"), i);
53		USplineMeshComponent * SplineMeshComponent = NewObject<USplineMeshComponent>(this, Name);
		if (IsValid(SplineMeshComponent) && IsValid(StaticMesh))
		{
			SplineMeshComponent->SetupAttachment(SplineComponent);
			SplineMeshComponent->SetSimulatePhysics(true);
			SplineMeshComponent->SetCollisionProfileName(TEXT("BlockAllDynamic"));
			SplineMeshComponent->SetMobility(EComponentMobility::Movable);
			SplineMeshComponent->SetStaticMesh(StaticMesh);
			SplineMeshComponent->SetForwardAxis(InForwardAxis);
			SplineMeshComponentArray.Add(SplineMeshComponent);
			SplineMeshComponent->RegisterComponent();
		}
	}
}

Hey there, the error is somewhere inside the for loop, try executing the engine using visual studio and let me know what is the exact line it crashed.

It can’t be on the for itself, try to comment everything inside the for loop and see if that works. If it still crashes comment the printstring and uelog you have before the for loop.

Crashed in the 50 line

When the ‘SetStaticMesh’ function is commented then is not being crashed UE4

61. SplineMeshComponent->SetStaticMesh(StaticMesh);

Yes, When I comment SetStaticMesh it doesn’t crash .

Snake.h file

UPROPERTY(EditAnywhere, Category = "Static Mesh")
UStaticMesh * StaticMesh;

Snake.cpp file

StaticMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Static Mesh"));

I want to set static mesh from Blueprint.

When you comment SetStaticMesh it doesnt crash?

What is the value of StaticMesh that you pass on the SetStaticMesh?

Can you debug and see the actual value of static mesh?

Good to know :slight_smile: Please mark my answer below as correct and upvote it if you found it helpful :slight_smile:

I have found how to solve the problem.
I have added the condition.

if( StaticMesh->RenderData != nullptr) 
{
    SplineMeshComponent->SetStaticMesh(StaticMesh);
}

Thank you.