Seemingly Random EXCEPTION_ACCESS_VIOLATION thrown after random amount of time playing in editor

Hi, I’ve been writing a state system based on the state pattern using cpp for a character in ue5.

I have come across an error with the engine where it seems to crash due to an EXCEPTION_ACCESS_VIOLATION after a random amount of time while doing nothing in particular. I understand that this error means that there was some memory that the engine attempted to access and failed. Every time it crashes there could be a different result. I have noticed a few different recurring crash logs.

1. The first crash log that I will receive points to a line in my code. The hex value would be one of the following: 0x00000000, 0xfffffffffff, or a seemingly random value. This originally caused me to believe that I was attempting to call a function on a nullptr value. This is the code segment that I am pointed to:

void ATimeTraveler::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if (CurrentState != nullptr) {
		assert(CurrentState != nullptr);
		CurrentState->Update(); // this is the line that the error points to
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("Current State is nullptr"));
	}
}

ATimeTraveler is a class which extends the basic ACharacter class. The line in my code is surrounded in a conditional where it will only run given that CurrentState != nullptr and I assert that it is not nullptr. I will still receive this version of the crash log despite this.

2. The other type of crash log i will receive will similarly point to either a 0x0000000, 0xffffffffff, or seemingly random value, but will not mention any lines from my written code, but only values such as UnrealEditor_Engine and UnrealEditor_Core.

I am completely lost as to why I could be getting this error. Please let me know if you would like me to provide anymore information. Thank you for any help in advance!

Does you CurrentState variable marked as UPROPERTY? If CurrentState type is UObject child, it can be garbage collected at any time and this value will be invalid.

1 Like

I do not declare it as a UPROPERTY.
Here is the line in my character class header file
UBaseState* CurrentState;

This is the header file for the UBaseState class:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

class ATimeTraveler;

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "BaseState.generated.h"

/**
 * 
 */
UCLASS(Abstract, BlueprintType, Blueprintable)
class TIMEJUMP_API UBaseState : public UObject
{
	GENERATED_BODY()
	
public:

	UBaseState();
	~UBaseState();

	FString StateType;

	virtual void Update();
	virtual UBaseState* HandleInput(ATimeTraveler*, FString);
	virtual void Enter(ATimeTraveler*);

	UFUNCTION(BlueprintCallable)
	FString GetStateType();

	UBaseState* DetermineNewState(UBaseState*, ATimeTraveler*);
};

If I am understanding correctly, what you think might be the problem is that because I did not mark the variable as a UPROPERTY, UE5 isn’t handling the data as expected, and could be sending the data to the garbage collector preemptively?

I will try to make these changes and will post an update when I finish testing.

I believe not declaring them with UPROPERTY was the issue. Thank you so much! Something to note was that I had to move my pointer definitions to inside the BeginPlay function rather than the actor constructor once I declared them with the UPROPERTY() macro