UE4 Crash on start EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000

Hi all,

Ultra noob with another silly question.

Code was working fine, tried to add an audio component to a base class, compiler threw a heap error, Google told me to go into .target.cs to increase heap memory, did that and the whole thing started crashing with the error in the question subject header. Went back and undid everything, but the error remains. I gather that the code is usually related to hanging pointers in the class constructors so I’m including the header and cpp code below in case anyone can help me spot something egregious:

// Header file.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnBase.generated.h"

class UCapsuleComponent;
class AProjectileBase;

UCLASS()
class TOONTANKS_API APawnBase : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	APawnBase();

private:
	// components
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
	UCapsuleComponent* CapsuleComp = nullptr;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))	
	UStaticMeshComponent* BaseMesh = nullptr;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))	
	UStaticMeshComponent* TurretMesh = nullptr;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
	USceneComponent* ProjectileSpawnPoint = nullptr;
	// variables
	// Limits options to only those in the projectile class i.e. C++ ProjectileClass or BP version
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile Type", meta = (AllowPrivateAccess = "true"))
	TSubclassOf<AProjectileBase> ProjectileClass;

protected:
	void RotateTurret(FVector LookAtTarget);
	void Fire();
	virtual void HandleDestruction();
};

```// Cpp


#include "PawnBase.h"
#include "Components/CapsuleComponent.h"
#include "ToonTanks/Actors/ProjectileBase.h"

// Sets default values
APawnBase::APawnBase()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule Collider"));
	RootComponent = CapsuleComp;

	BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base Mesh"));
	BaseMesh->SetupAttachment(RootComponent);

	TurretMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Turret Mesh"));
	TurretMesh->SetupAttachment(BaseMesh);

	ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("Projectile Spawn"));
	ProjectileSpawnPoint->SetupAttachment(TurretMesh);
}

void APawnBase::RotateTurret(FVector LookAtTarget) 
{
	FVector LookAtTargetCleaned = FVector(LookAtTarget.X, LookAtTarget.Y, TurretMesh->GetComponentLocation().Z);
	FVector StartLocation = FVector(TurretMesh->GetComponentLocation());
	FRotator TurretRotation = FVector(LookAtTargetCleaned - StartLocation).Rotation();

	TurretMesh->SetWorldRotation(TurretRotation);
}

void APawnBase::Fire() 
{
	if (ProjectileClass)
	{
		FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
		FRotator SpawnRotation = ProjectileSpawnPoint->GetComponentRotation();
		AProjectileBase* TempProjectile = GetWorld()->SpawnActor<AProjectileBase>(ProjectileClass, SpawnLocation, SpawnRotation);
		// What does this do?
		TempProjectile->SetOwner(this);
	};
}

void APawnBase::HandleDestruction() 
{
	
}


Apologies if this is a dumb question but I’m out of ideas and things to Google.

Thanks!

I know that someone had a similar issue recently and removing this stopped the error:

Tried but still no dice. Thanks anyway!

Two other things to try is add : Super() to your constructor in cpp

also add

#include "Components/StaticMeshComponent.h"

If that still doesn’t work I’d delete the Binaries and Intermediate folders and regenerate the project.

I am currently doing the same tutorial and checked your BasePawn class with mine and both look similar. So I would try what SolidGasStudios said about regenerate project and if that doesn’t work, search the problem in another classes.

The only difference I found at your fire function is that you use:
AProjectileBase* TempProjectile = GetWorld()->SpawnA…
and I use:
auto TempProjectile = GetWorld()->SpawnA…

Edit: At your fire function, at the if (ProjectileClass), you end it with this }; , remove the ; , if and else brackets don’t use ; , you crazy person!

Thanks for the suggestions! The comile error pointed to this cpp file to the white space immediately after the constructor and I have been through the other classes to double check. Deleted Intermediaries and Binaries, tried rebuilding but it failed and suggested I build from source, which I only know hoe to do with the Intermediaries files in place. THIS IS SO ANNOYING! Inevitably it’s something stupid I’ve done but I really can’t figure out what it might be!

PS. Thanks for the semicolon catch. I be crazy sometimes!

Update: I finally figured out to build from inside VSCode. The only errors it threw were that I couldn’t use BlueprintReadOnly from within a private member. Google told me that’s what meta = (AllowPrivateAccess = “true”) was for so I re-wrote this in in all the places vscode told me to, rebuit and now I’m back in.

All I can say to any future strugglers reading this is to make sure that any pointer declared as a private member with any reference to blueprint access should also include the meta line too.

Thanks to all above for your help. We move!

1 Like