Correct pattern for caching/updating component pointers

We’re seeing some log warnings about AccessViolation in property accessors not finding valid components or component data fields. These happen consistently in the preview port, but do not seem to be affecting our PIE runs.

We have component pointers on our AActor derived types and our UActorComponent derived types.

UPROPERTY(BlueprintReadOnly, Transient)

TObjectPtr<UGameTypeComponent> GameTypeComponent;

For the AActor derived type, we noticed that we need to re-initialize our pointers in the ReregisterAllComponents() overridden function, as they may have been unregistered and new ones registered.

For the UActorComponent derived type, when the component is unregistered and a new one registered the InitializeComponent function is not called, so our initialization of those pointers needed to move to our OnRegister() override.

Is this the correct pattern to use, or is there another pattern we should be using?

Steps to Reproduce
Previewing an actor generates a bunch of access violation warnings about property accessors.

Running in PIE does not have this issue.

Hi,

Your pattern for caching and updating component pointers looks good.

To add a bit of redundancy for the AActor derived type, you could consider reinitializing the pointers in PostInitializeComponents() to make sure they’re valid after component registration.

I believe the access violations in preview port can be mitigated by adding IsValid() or null checks before accessing the object and components.

Regards