Certain code causes crashes

I’m new to programming in UE4, I’m not sure how it all works. All I’ve gathered so far is that doing things causes crashes.
I’ve set up the following public property and functions in my actor class as follows:

Territory.h:

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* terrMesh;

UFUNCTION(BlueprintCallable, Category="functions")
void GenerateArmy(int32 troops);

UFUNCTION(BlueprintCallable, Category = "functions")
void  ChangeMesh();
	
UFUNCTION(BlueprintCallable, Category = "thing")
int32 testFunction();

Territory.cpp:

ATerritory::ATerritory()
{
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> meshObj(TEXT("/Game/Geometry/Meshes/TemplateFloor.TemplateFloor"));
	terrMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("placeolder"));
	terrMesh->SetStaticMesh(meshObj.Object);
}

void ATerritory::BeginPlay()
{
	Super::BeginPlay();
	
        //Copy-pasted generateArmy
	FVector loc = this->GetActorLocation();
	GetWorld()->SpawnActor<AActiveArmy>(GetClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);
}

void ATerritory::Tick( float DeltaTime )
{  //snip  }

void ATerritory::GenerateArmy(int32 troops)
{
	FVector loc = this->GetActorLocation();
	/*armyDood =*/ GetWorld()->SpawnActor<AActiveArmy>(GetClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);
	armyDood->troopCount = 42;
}

void ATerritory::ChangeMesh()
{
	static ConstructorHelpers::FObjectFinder<UStaticMesh> fountainObj(TEXT(
		"/Game/InfinityBladeGrassLands/Environments/Plains/Env_Plains_Ruins/StaticMesh/SM_Plains_Castle_Fountain_01.SM_Plains_Castle_Fountain_01"));
	//terrMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("newMesh"));
	terrMesh->SetStaticMesh(fountainObj.Object);
}

int32 ATerritory::testFunction()
{
	return 0;
}

I’ve tried calling each of the three functions via a trigger blueprint. GenerateArmy and ChangeMesh cause a crash whenever they’re called but the TestFunction executes fine.

I’ve also tried directly calling both the functions and the code contained in the functions in BeginPlay. Every time I got a crash. This leads me to believe that there’s something wrong with the way I’m writing the code, not something wrong with the blueprints.

Also, here’s the ActiveArmy class:

ActiveArmy.h:

protected:

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta=(AllowPrivateAccess = "true"))
	class UTextRenderComponent* troopText;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
	class UStaticMeshComponent* armyMesh;

public:	
	default stuff

	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	int32 troopCount;

	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	int32 teamNum;
	
	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	class ATerritory* homeTerritory;

ActiveArmy.cpp:

AActiveArmy::AActiveArmy()
{
	FVector loc = this->GetActorLocation();

	PrimaryActorTick.bCanEverTick = true;

	//Set up static mesh
	static ConstructorHelpers::FObjectFinder<UStaticMesh> meshObj(TEXT("/Game/Geometry/Meshes/1M_Cube.1M_Cube"));
	armyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("meshObj"));
	armyMesh->SetStaticMesh(meshObj.Object);

	//Set up text render
	troopText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("TextRender"));
	troopText->SetText(FText::AsNumber(troopCount));
	troopText->SetRelativeLocation(loc + FVector(0.0f, 0.0f, 20.0f));

}

void AActiveArmy::BeginPlay()
{
	Super::BeginPlay();

	if (homeTerritory)
	{
		FVector newLoc = homeTerritory->GetActorLocation() + FVector(0.0, 0.0, 20.0);
		SetActorLocation(newLoc);
	}

Lines like this

GetWorld()->SpawnActor<AActiveArmy>(GetClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);

GetClass() is returning the class of the caller – but the template parameter you’re passing to SpawnActor is for a different class. Basically, you’re telling SpawnActor to expect the UClass you’re passing it to be a AActiveArmy, but you’re passing it a UClass for a ATerritory, because an instance of ATerritory is calling GetClass() on itself. You want to do something like

GetWorld()->SpawnActor<AActiveArmy>(AActiveArmy::StaticClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);

instead. (Note: I didn’t check this code, this might be the wrong way to call it, let me know if it doesn’t work).

Another problem:

 void ATerritory::ChangeMesh()
 {
     static ConstructorHelpers::FObjectFinder<UStaticMesh> fountainObj(TEXT(
....

You can’t use FObjectFinder outside of a UObject’s constructor. UE4 will fail an assert and terminate.

Both of these errors you’re showing are not segfaults. They are failing assertions. This is the correct behavior of UE4, because you are making a mistake, and it’s trying to let you know. You can see the error message if you run it from Visual Studio with a debugger, or if you look at the crash reporter text.

Ok, so it was just me having no idea what I was doing. Thanks for clearing that up.

No problem. Could you re-mark it as answered? (Answerhub removes the ‘answered’ flag if you reply to the answer)