Must Each TObjectPtr<T> Be Marked UPROPERTY()?

I have some classes where I don’t want to denote TObjectPtr UPROPERTY(), but the IDE tells me that this object can be deleted during garbage collection. Is this true, or can it be disregarded.

Example:


I don’t need to interact with this object in the blueprint.

Thanks for your help.

If you don’t need UPROPERTY() for blueprint, do this.

UPROPERTY()
TObjectPtr<UUserWudget> WidgetForDisplayInstance;

Because your class is a widget and gets checked during garbage collection. Without UPROPERTY() to manage the smart pointer count. It can get deleted.

And not every TObjectPtr needs UPROPERTY(). It’s just widgets are special during GCs.

1 Like

Which ones don’t need it? Any raw pointer that is not marked as a UPROPERTY is unknown to the GC which should include all UObject pointers.

When you want to manage the pointer yourself and use it like a raw pointer. But want to use its template functions instead of writing your own. It is safer to use UPRPERTY(), yes.

I can’t think of an example now but the answer to everything programming is always “it depends”.

The UPROPERTY() macro is neede only if the class you’re writing has got the ownership of that pointed object.

Can you give an example when you would not use UPROPERTY on a UObject Pointer?
If the Property is not taking ownership surely you would use a TWeakObjectPtr and not a raw pointer that won’t be set to null by the GC due to a missing UPROPERTY macro?

Example 1: if you have an actor class where you instantiate an UObject, then it should be a UProperty if you don’t want it will become a dangling pointer.

UCLASS()
class AMyActor : public AActor
{
       GENERATED_BODY()
public:
       AMyActor()
       {
             MyObject = CreateDefaultSubobject<UObject>(TEXT("MyObj"));
       }

private:

     UPROPERTY()
     TObjectPtr<UObject> MyObject{};
};

Example2: if you get a component from an actor and you hold its memory address in a pointer then you don’t need it is a UProperty, because you have not the ownership of that pointed object.

UCLASS()
class AMyActor : public AActor
{
       GENERATED_BODY()
public:

      virtual void BeginPlay() override
      {
           Super::BeginPlay();
           if(auto* CM = UGameplayStatics::GetPlayerCameraManager(this, 0))
           {
                   SceneComponent = CM->GetRootComponent();
           }
      }

private:

     UPROPERTY()
     TObjectPtr<USceneComponent> Component{};
};
1 Like

Thanks for the example however I would still choose a TWeakObjectPtr for example 2.

TWeakObjectPtr it’s ok for that case, but I suggest that you avoid to check it at tick time.

The alternative is a crash though or a UProperty so I would not worry about it at all as long as it is safe and easy to read.

Keep in mind if you declare all properties as UProperty, the UHT execution and the compile time will be very long.

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