Protecting our Pointers

In the last section, we were tought that is was important to protect our pointers. This included initialising all pointers to nullptr, logging errors if the pointers were still nullptr after being set in the .cpp code, and then including an a line saying if (!SomePointer) {return;} before any pointer that is being used in the code.

In this section, none of that is being done, and it hasn’t been mentioned if this is just an oversite, and we should do it as an extention excercise, or if we don’t need to protect the pointers because they all being used to create the class.

Should I be initalising all pointers in the PawnBase.h file to nullptr, and put a if statement before the uses of the pointers, e.g.

BaseMesh = CreateDefaultSubobject(TEXT(“Base Mesh”));
if (!BaseMesh)
{
UE_LOG(LogTemp, Error, TEXT("No BaseMesh found));
return;
}
BaseMesh->SetupAttachment(RootComponent);

Is it worth doing this as an extention excercise, or is protecting the pointers not needed here?

EDIT: As I went through later lectures, I noticed some pointers are protected using the if-return statement. I also realised that most of my questions about protecting pointers are in regards to components, which are initalised during compile time, rather than at run time (as I think I understand). So does that mean component pointers don’t need the same protection as other pointers?

It’s not finding it, it’s creating it. If it failed to create the component then you’ve got other issues e.g. ran out of memory and there’s not much else the program can do other than crash/terminate. So just treat CreateDefaultSubobject as always returning a non-null pointer.

Thank you for clarifying. It was clear at first what we were doing in that lecture, but in later lectures, I realised what was happening.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms