x

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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

[C++] How to prevent crashes when programming in UE4?

Hi, I need to know if there is some sort of cheatsheet about prevent variables crashing the engine, this because some things are perfectly fine when compiling, but on press play they silently will crash the engine. I wish to know if for example variables have a check like:

TArray.isValid or isValid(pointer)

or something else.

I want to avoid the hassle to check on crashes everytime view symbols or write code that compile fine but fail everytime.

Thanks for helping me!

Product Version: UE 4.21
Tags:
more ▼

asked May 07 '19 at 08:04 AM in C++ Programming

avatar image

thunder_nemesis
48 2 2 8

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

1 answer: sort voted first

The most common crash reason is nullptr instead of an actual object.

There are 2 main approaches I know:

  1. to build such an architecture which will make things sure to be available. It's a complex way and requires a senior-level experience to implement properly.

  2. Just check your pointers with IsValid(pointer) right after getting it and prepare something in case if pointer is not valid.

For instance, if you're in AController-derived class and going to use GetPawn() method:

 AActor* MyPawn = GetPawn();
 if (!IsValid(MyPawn)) {
     // just return if possible or maybe do some fallback here
     return;
 }

 // Do something with MyPawn here

Though there are no such checks for structures, since they are always exist, just can be with default values – so you need to make some manual checks for structures, if it's needed.

Also you should add the UPROPERTY() macro for any member derived from UObject (including AActor and so on) – it will make them work properly with garbage collection, otherwise you can face nullptr even when the object should exist.

Another common crash reason is accessing a TArray or TMap with an index which does not exist. For TArray there is the method for it: MyArray.IsValidIndex(Index). For TMap it's MyMap.Contains(Key).

more ▼

answered May 07 '19 at 08:42 AM

avatar image

norlin
430 11 18 25

avatar image thunder_nemesis May 07 '19 at 09:59 PM

Thank you very much :-) !

(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