Child instance sometimes get casted to interface instance and chrashes ue5.

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?

Hello :slight_smile:

I’ll try to help you with my answer but I think you should read this whole post about interfaces if you want to work with them UE4 C++ Interfaces - Hints n Tips · SteveStreeting.com

  1. From what I see your pointer to interface in component is not marked as UPROPERTY, unless you know what you’re doing this shouldn’t happen because UnrealGarbageCollector might delete your object when you are trying to use it, or first and foremost won’t make this pointer a null.
  2. Don’t store interface pointers in that way
IIndicatorInterface* Indicator;

you should either use

UPROPERTY(...)
TScriptInterface<IInterface>
or
UPROPERTY(...)
UObject* 
  1. You should send some more actuall code from cpp instead of comments because without it it’s hard to figure out something more.

BTW The error in 99% of times means that you’re trying to access nullptr so I think that what I wrote here might be enough to help you.

Thank you. This really helped. I tried to only include relevant code, to not bloat it to much.

1 Like