Crash when using virtual function and override

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.

Try to use UPROPERTY() for declaration of _speeches to avoid GC issues.

I was happy that this seemed to fix the issue, until it came up again.

If I compile in Visual Studio and open and run the game in the editor, it works perfectly fine now. I can stop and play the game and it works. However, if I change anything in the code (such as modifying a debug message to the screen) and then do a hot compile in the editor, the crash occurs and in the same location. As before, the difference between working and not working is consistent.

Why would compiling in the editor make a difference compared to compiling in Visual Studio?

I have many issues with ‘hot reload’ feature and I am not reccomend to use it. Build with Visual Studio with closed editor.

I agree that compiling in Visual Studio is better than the hot reload feature. I’m just worried that the crash is indicative of a problem that may rear its head later when it’s harder to narrow down. If anyone else has any insight into the differences between compilations and how to tackle them I’m open to suggestion.

At any rate, thank you v.s. for the advice on setting the TArray as a UPROPERY.