This project is a test to see how UE4 memory management works with UPROPERTY pointers, naked pointers, and TWeakObjectPtr.
The test uses two actors in a level. One of the actors creates it's own components and the second actor refers to the first actors components.
The first actor is then deleted so the effects on the second actor can be observed.
- Play in Editor
- Wait for a few seconds and notice the error messages. These are from TArray's that were no properly marked as UPROPERTY.
- Delete the DeleteMe object in the World Outliner
- Press stop.
- Look at the Output Log.
- Naked pointers are not nulled out, but IsValidLowLevel returns false.
- UPROPERTY marked pointers and TWeakObjectPtr are nulled out.
- The UObjects in TArrays marked with UPROPERTY are kept around, while UObjects in TArrays NOT marked with UPROPERTY are garbage collected.
If you hold onto an object you are not the owner of then either store it in a TWeakObjectPtr or UPROPERTY marked pointer and do a null check before access.
If you own the object - it was created with CreateDefaultSubobject in the constructor or NewObject(this) somehwere else in your code, then naked pointers are fine (since you know when they will become invalid).
If you have a TArray member of a UCLASS or USTRUCT, mark it as a UPROPERTY if it is going to hold any UObject sub-class, otherwise the UObjects could be garbage collected.