Crashing due to new Actor class

You are using 2 contructors in you actor class. I’m not sure if that’s the problem.

// remove this
ACharWeapon();

ACharWeapon(const class FObjectInitializer& FOI);

you also forgot to add the UPROPERTY macros to the CurrentMesh and StaticMeshComponent. Without them they get garbage collected and accessing them isn’t safe at all. nullptr crash.

Thanks for the response,

I tried both fixes you suggested, re-compiled within visual studio (which was successful), but the project still failed at the same point. Interestingly, it still claims ACharWeapon()::ACharWeapon() is the problem within the log file, which is very confusing? The only change is it now claims the error is at line 31 in the new cpp file, which contains this line:

RootComponent = StaticMeshComponent;

Here is the new log file:
link text

Also, do you need to declare all your variables as UPROPERTY for that reason, or is it just specific ones?

Thanks for your help

The problem is apparently due to this code in the constructor:

//Find the associated mesh
     static ConstructorHelpers::FObjectFinder<UStaticMesh> tempObject(TEXT(GunPath));
     if (tempObject.Succeeded())
     {
         CurrentMesh = tempObject.Object;
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT(
             "Weapon: Successfully loaded Gun mesh into weapon instance")));
     }
     else {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(
             "Weapon: Failed to load Gun Mesh")));
     }
 
     //Set the Weapons' active mesh to the saved mesh
     StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
     RootComponent = StaticMeshComponent;

By removing this code and putting it in the Begin Play function, I can now access the engine, however it now fails on pressing play, I believe this to be due to not being able to use FObjectFinder outside of a constructor?

No you have to use ConstructorHelpers::FObjectFinder in your constructor.

you have to use make all pointers with the UPROPERTY macro that need to be managed by garbage collection. Otherwise you run the risk of your objects getting removed from memory and getting a nullptr crash.

Hi,

The project I was working on worked fine before I closed it, however upon restarting it I get a crash at 71% loading of the file. The code components compile correctly within visual studio, however the log file seems to claim the new class (ACharWeapon) is the problem. I tried deleting it within visual studio, but I don’t think that deleted it from the project, and when I do delete it within the project the rebuilding after deleting binaries, intermediates and saved does not work properly.

Before I closed the engine, the actor was spawning properly within the level, however the spawning code was deleted to see if that allowed me to open the project (The character class only contains two pointers to weapons now, which are never used or filled).

Log File

Header File for the problematic class:

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Engine/StaticMesh.h"
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
#include "CharWeapon.generated.h"

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

	ACharWeapon(const class FObjectInitializer& FOI);

	UStaticMesh* CurrentMesh;

	UStaticMeshComponent* StaticMeshComponent;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	
	
};

Source Code for the problematic class:

#include "CharWeapon.h"
#include "Engine.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"

#define GunPath "/Game/Characters/Weapons/GunStatic"

// Sets default values
ACharWeapon::ACharWeapon()
{
	Super();

 	// Set this actor to call Tick() every frame
	PrimaryActorTick.bCanEverTick = true;

}

// Sets default values when used within a actor spawn
ACharWeapon::ACharWeapon(const class FObjectInitializer& FOI)
	: Super(FOI)
{
	// Set this actor to call Tick() every frame
	PrimaryActorTick.bCanEverTick = true;

	//Find the associated mesh
	static ConstructorHelpers::FObjectFinder<UStaticMesh> tempObject(TEXT(GunPath));
	if (tempObject.Succeeded())
	{
		CurrentMesh = tempObject.Object;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT(
			"Weapon: Successfully loaded Gun mesh into weapon instance")));
	}
	else {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(
			"Weapon: Failed to load Gun Mesh")));
	}

	//Set the Weapons' active mesh to the saved mesh
	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	RootComponent = StaticMeshComponent;
}

// Called when the game starts or when spawned
void ACharWeapon::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ACharWeapon::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

Thanks for any and all help :slight_smile: