Interface instance crash
Classes
I have an ActorComponent with the the .hpp
class MYGAME_API UIndicatorComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UIndicatorComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
void SetIndicator(IIndicatorInterface* indicator);
private:
IIndicatorInterface* Indicator;
};
.cpp of the IndicatorComponent:
// Sets default values for this component's properties
UIndicatorComponent::UIndicatorComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UIndicatorComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UIndicatorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (Indicator == nullptr)
{
return;
}
if (Indicator->IsIndicatorDeletable()) // HERE IS THE POINT WHERE THE EDITOR CRASHES
{
Indicator->ResetSystem();
Indicator = nullptr;
}
else
{
Indicator->DisplayIndicator();
}
}
void UIndicatorComponent::SetIndicator(IIndicatorInterface* indicator)
{
Indicator = indicator;
}
The IIndicator .hpp:
class UIndicatorInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class MYGAME_API IThis text will be hiddenIndicatorInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual void DisplayIndicator();
virtual void ResetSystem();
virtual bool IsIndicatorDeletable();
};
The IIndicatorInterface .cpp:
void IIndicatorInterface::DisplayIndicator()
{
}
void IIndicatorInterface::ResetSystem()
{
}
bool ISpellIndicatorInterface::IsIndicatorDeletable()
{
return false;
}
Child class of IIndicatorInterface (.hpp):
class MYGAME_API USpecificIndicator : public UObject, public IIndicatorInterface
{
GENERATED_BODY()
public:
USpecificIndicator();
virtual void DisplayIndicator() override;
virtual void ResetSystem() override;
virtual bool IsIndicatorDeletable() override;
}
The .cpp of SpecificIndicator:
void USpecificIndicator::DisplayIndicator()
{
// does some staticmeshactor stuff
}
void USpecificIndicator::ResetSystem()
{
// deletes staticmeshactor
}
bool USpecificIndicator::IsIndicatorDeletable()
{
//checks if the indicator should be deleted after it is not needed anymore
}
The problem
The Attribute Indicator
in the ActorComponent class is an USpecificIndicator instance. In the beginning the Indicator works fine and it displays a StaticMeshActor as I want, but at some point the editor crashes without any input on my side.
The error message:
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000005900000078
My diagnosis attempt
I used the debugger and checked the Tick function of the InterfaceComponent at the point right befor the editor crashes. At this point the Attribute Indicator
is no longer an instance of USpecificIndicator
but it is an Instance of IIndicatorInterrface
. I understand that you cant have an instance of an interface (hence the crash), but I dont understand how the instance can change from normal class to interface without me casting it. How can this happen?