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.
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”.
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.