I have a TArray of pointers to objects:
TArray Modules;
In my class constructor, I do:
Modules.Reset();
Since from what I can glean by looking at sample code, this is a reasonably safe way to initialize/zero-out the array. At some point right after level start, I add a single element to the array. So, it should at least have length 1. Then some point later on (1 second later actually, via timer), I execute the following code:
UModule* ASagittarius::GetModule(FString ID)
{
int32 i = 0;
bool found = false;
LogDebug(FString::Printf(TEXT("Number of currently loaded modules: %d"), Modules.Num()));
for (i = 0; i < Modules.Num(); i++)
{
if (Modules[i]->ID == ID)
{
found = true;
break;
}
}
if (!found)
{
LogWarn(FString::Printf(TEXT("GetModule() - module with ID %s does not exist"), *ID));
return NULL;
}
return Modules[i];
}
But I always get an EXCEPTION_ACCESS_VIOLATION whenever I call Modules.Num()
. I have been over the code dozens of times now and can’t catch what I did wrong, and furthermore this is the same idea employed in several places in ShooterGame.
My array should have an element in it (only I don’t know if it does, since Num()
errors out). So what is going on here?
EDIT: Here’s the entire contents of Module.h which defines the “UModule” class:
// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "SagResponse.h"
#include "Sagittarius.h"
#include "Module.generated.h"
/**
*
*/
UCLASS(Abstract)
class UModule : public UObject
{
GENERATED_UCLASS_BODY()
FString ID;
ASagittarius* Parent;
public:
virtual void Initialize(ASagittarius* NewParent);
void SubmitAction(FString QueryID, UAction* ToSubmit);
virtual void OnResponseReceived(FString ActionID, USagResponse* resp);
};
Please note that even though the class is abstract, pointers to its various subclasses are what is actually getting stored in the array. They are then dynamically cast back when returned from the GetModule()
function.
EDIT 2: Header for Sagittarius.h
, where the array is defined:
// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Sagittarius.generated.h"
// Other stuff (omitted for brevity)
/**
*
*/
UCLASS()
class ASagittarius : public AActor
{
GENERATED_UCLASS_BODY()
USagittariusLinkClient* Link;
FString SagPass;
TArray Modules;
public:
void Initialize(FString AppID, FString Pass);
UAction* CreateAction(FString Type);
void RegisterModule(UModule* m);
UModule* GetModule(FString ID);
// Other functions...(omitted for brevity)
};