Background
I have two simple UFUNCTIONS in a class which holds a TArray:
GetScheduleItemAt(int arrayloc) // gets the items at an array location for Blueprints
GetSize() // simply returns the TArray.Num
I go ahead and insert 24 class pointers into the array at BeginPlay and then every frame I call GetSize() to make sure the TArray size > 0, and if so, I get a specific item at a location.
However, after the game runs for about 1 minute (consistently this happens every time), GetSize() returns -572662307 instead of 24, making me assume it’s been deallocated.
Does anyone know why this might be? I have tried with the TArray both as a UPROPERTY() and not, so it appears to make little difference.
Here is the class and its header file:
.cpp:
#include "Warlock.h"
#include "Schedule.h"
// Called when the game starts or when spawned
void ASchedule::BeginPlay()
{
Super::BeginPlay();
Schedule = TArray<UScheduleItem*>();
Schedule.Reserve(24);
}
void ASchedule::InsertScheduleItem(UScheduleItem* si)
{
int start_hour = si->GetStartHour();
int end_hour = si->GetEndHour();
if (start_hour < MIN_HOUR_IN_DAY || start_hour > MAX_HOUR_IN_DAY)
return;
if (end_hour < MIN_HOUR_IN_DAY || end_hour > MAX_HOUR_IN_DAY)
return;
for (int i = start_hour; i < end_hour; i++)
Schedule.Insert(si, i);
}
void ASchedule::PrintDebug()
{
UE_LOG(LogTemp, Warning, TEXT("Schedule Printing: "));
for(UScheduleItem* si : Schedule)
{
const UEnum* temp = FindObject<UEnum>(ANY_PACKAGE, TEXT("CitizenState"));
UE_LOG(LogTemp, Warning, TEXT("Task: %s, StartHour: %d, EndHour: %d"), *(temp ? temp->GetNameStringByIndex((int32)si->task) : TEXT("Invalid Enum")), si->GetStartHour(), si->GetEndHour());
}
}
UScheduleItem* ASchedule::GetScheduleItemAt(int loc)
{
return Schedule[loc];
}
int ASchedule::GetSize()
{
return Schedule.Num();
}
.h:
#pragma once
#include "GameFramework/Actor.h"
#include "ScheduleItem.h"
#include "Schedule.generated.h"
const int MIN_HOUR_IN_DAY = 0;
const int MAX_HOUR_IN_DAY = 24;
/**
*
*/
UCLASS()
class WARLOCK_API ASchedule : public AActor
{
GENERATED_BODY()
public:
void InsertScheduleItem(UScheduleItem* si);
void PrintDebug();
UScheduleItem* GetScheduleItemAt(int loc);
int GetSize();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY()
TArray<UScheduleItem*> Schedule;
};