Search code examples
c++crashunreal-engine4

GetOwner() cause unreal editor crash


I'm new in using c++ and UE. I tried some simple programming but the editor crash.

#include "NewActorComponent.h"
#include "Runtime/Engine/Classes/GameFramework/Actor.h"

 UNewActorComponent::UNewActorComponent()
 {

     PrimaryComponentTick.bCanEverTick = true;
     GetOwner()->GetName();
 }

I knew maybe the output is null so it is crashed,but idk how to expect the error without any crash.


Solution

  • It's possible the actor component is created but not initialized or attached to an object. You should gate these kind of checks behind IF statements, or use the assert/check macros.

    Also, you may want to use the BeginPlay() function instead of the constructor. BeginPlay requires the component to be registered and initialized so it should have an owner.