I am currently learning more about pointers and read, that UPROPERTY pointers to UObjects are automatically updated to NULL, if the UObject is destroyed and and TWeakObjectPtrs can be checked for validity. I assume normal, raw pointers could potentially become invalid, i.e. point to something other than NULL or a valid object. Are raw pointers therefore dangerous to use, if I store them for some time, so that some other class could delete the UObject the pointer points to? And if so, should I always use UPROPERTY pointers or TWeakObjectPtrs, when I am storing pointers for a short or long duration?
Secondly is it good practice or even possible to use TSharedPtr for UObjects? And what should I do if I need a weak UPROPERTY pointer, that does not prevent garbage collection? Or what if I need a non-UPROPERTY pointer, that prevents garbage collection?
Yes, anytime you hold a pointer across multiple frames, you need to use one of the above two methods.
Nope, they’re for non-UObjects only.
UPROPERTY()
TWeakObjectPtr< UMyObject > Ptr;
Rarely needed - you can declare a UPROPERTY as above without exposing it to blueprint, editor, etc, so there is rarely an advantage to it not being a UPROPERTY. If you need to store it within something that is not a UCLASS/USTRUCT and therefore can’t use UPROPERTY, you can derive your class from FGCObject. There are lots of examples in the engine codebase.