Is it safe to store AActor without UPROPERTY?

Welp, new fear unlocked! I’m going to have to make some refactoring.. Thanks for the reminder. It should be taken especially seriously that, such an error being quite low-probability to trigger, it may be invisible during development, and emerge only upon release with a large enough sample size.. The worst kind of error.

As for the original question, I’m with MagForceSeven here, you should avoid using a pointer to an array if you don’t have enough mastery over both generic C++ & UE-specific memory management. (And actually, master over the lifecycle of both the actual array owner & the array reference user)

I’m not going to say that it’s never useful (one never knows). But that seems to be a needlessly dangerous way of doing things.

Instead, I would recommend you to store a reference to the array’s owner, and then call the array directly through the owner, every time.

// .h
UMyArrayOwner* ArrayOwner;

// .cpp
if (/*ArrayOwner is not null & valid etc etc... Defensive programming boilerplate*/) {
    DoStuff(ArrayOwner->ControlledActors);
}

In terms of actual performance, the difference between this method & your original method, OP, should be assumed to be overkill optimization, unless you have a (very rare) provable, measurable issue.

Of course, you can also use smart pointers if you know how to use them.

2 Likes