Is `TStrongObjectPtr` safe to copy?

I had always assumed that TStrongObjectPtr worked similarly to TSharedPtr in that it keeps an internal reference count. But it appears that it works more like TUniquePtr in that it takes exclusive ownership over the UObject.

So if you have two TStrongObjectPtr s referring to the same UObject and one of them goes out of scope, the other one now has a dangling pointer. Unless I misunderstand the code, this is because the base class GCObject unregisters the object in its destructor.

The copy constructor and copy assignment operators of TStrongObjectPtr do not take this into account though, so it’s easy to end up in this situation.

Am I on the wrong track here? If not, those copy operations should be removed

Yes. UObjects are garbage collected by another system, not allocated and owned by the TStrongObjectPtr in the same way as with a unique pointer. For UObjects, the Strong Ptr modifies a reference count on the the UObject itself which prevents the object from being destroyed purely for garbage collection reasons.

However, TStrongObjectPtr aren’t entirely foolproof since they can’t keep a UObject alive that otherwise has to be destroyed for some other reason. If you call AActor::Destroy or UActorComponent::DestroyComponent, those objects are going to go away no matter how many strong pointers you have pointing at them.

They are perfectly safe to copy.