Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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)

 // 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
 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.

Product Version: UE 4.9
more ▼

asked Apr 20 '16 at 05:06 AM in C++ Programming

avatar image

54 5 20 20

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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

more ▼

answered Apr 20 '16 at 07:31 AM

avatar image

347 10 12 27

avatar image sseufert Apr 21 '16 at 04:17 AM

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?

avatar image v.s. Apr 21 '16 at 09:34 PM

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

avatar image sseufert Apr 21 '16 at 10:59 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question