Search code examples
c++pointersunreal-engine4

Checking pointers before using? necessity and alternatives, C++ UE4


I'm new to UE4, and I encountered the general pattern of checking each pointer before using it, for example:

AActor *pActor = getOwner(); 
if(pActor){
   pActor->SOME_FUNCTION();
}

I'm aware to the fact that crashes are awful in terms of user experience, and that it is always advised to write code with maximum robustness, but are there well-known cases in which the pattern is really useful? (for example in theoretically safe settings, such as a pointer set to GetOwner()).

And if so, are there any common alternatives for this? (perhaps making use of smart pointers?)

EDIT: In my question I wanted to get an answer regarding UE4 specifically, since I encountered a source relating to UE4 only, which advised me to check pointers, although I wasn't sure about its necessity and in which pattern.


Solution

  • If you are not absolutely guaranteed to receive a valid pointer from a function that returns a pointer, you need to check that pointer. In the case of libraries that are black boxes (you don't own the code), you can perhaps take the word of the library author that the pointer will always be valid, but based on a comment above, that does not appear to be the case here.

    Interestingly enough, in the C++ Core Guidelines: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl

    There is a template in the Guidelines Support Library called not_null<T> which can be used to encapsulate a pointer to provide the guarantee that the pointer is never NULL.