Greetings. I’m getting an unhandled exception crash when I run a function as virtual but I don’t get the crash if I’m not running a function as virtual. I’m wondering if perhaps there are any quirks about UE4’s virtual functions that I’m unaware of.
In my game, one of my actor classes has a TArray of USpellSpeech pointers. USpellSpeech is a base class and I’m only using child classes of USpellSpeech. In my base spell speech class, if I write my OnHit function as a standard function, the game does not crash but accesses the base function (not what I want). If I write the function as virtual, with the child classes having a virtual override of that function, the game crashes. I am trying to call the child version of the function.
Header class of actor:
TArray<USpellSpeech*> _speeches;
bool AddSpeech(USpellSpeech* speech);
cpp of actor:
bool ASpell::AddSpeech(USpellSpeech* speech)
{
_speeches.Add(speech);
}
USpellSpeech.h:
// Doesn't crash, but function call enters this function instead of child's
void OnHit(AActor* hitActor);
// crashes on call
virtual void OnHit(AActor* hitActor);
UImmolate.h // UImmolate is a child of USpellSpeech
// doesn't crash, but never enters upon function call
void OnHit(AActor* hitActor);
// Overrides USpellSpeech's OnHit(); causes the crash
virtual void OnHit(AActor* hitActor) override;
In constructor of one of the actor class:
// UImmolate is a child of USpellSpeech
AddSpeech(NewObject<UImmolate>(UImmolate::StaticClass()));
Location of crash:
if (_overlappingActors.Num() > 0)
{
for (AActor* actor : _overlappingActors)
{
int numberOfSpeeches = _speeches.Num();
for (int index = 0; index < numberOfSpeeches; index++)
{
_speeches[index]->OnHit(actor); // CRASH OCCURS
}
}
}
The error I receive when using the virtual version of OnHit() is Unhandled Exception (UE4Editor-GameName-Win64-DebugGame.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.
Normally when I see this error, I assume to look for something being uninitialized, but all I’ve changed is whether or not the function is virtual (I can consistently go back and forth with the change and crash). Additionally, while debugging AddSpeech(), the parameter comes through as a properly constructed UImmolate and not as a base SpellSpeech, so both construction and casting seems to be working fine.Oh, and both "this"and the actor that is passed in to OnHit is also valid.