How can I get a container of pointers to pointers to UObjects?

My original design had the following declarations:

std::vector<AActor**> ActorsHolding;
AActor** CurrentActorP;

But, as far as I can tell, my pointers are not accounted for in UE4’s garbage collection and memory re-allocation, so they point to garbage quite frequently, which is causing issues.

The attempted solution is to slap UPROPERTY in front of them, and switch the vector to a TArray:

UPROPERTY() TArray<AActor**> ActorsHolding;
UPROPERTY() AActor** CurrentActorP;

but this produces an error:

H:/MyGame/Source/MyGame/AFolder/MyActorComponent.h(40): error : Missing ‘*’ in Expected a pointer type
so, instead I tried using a TSharedPtr < TSharedPtr < AActor > > . That didn’t work though, since TSharedPtr can only be used to point to non-UObjects, and TSharedPtr < AActor > is a UObject.

I’m considering a TSharedPtr < AActor* > , and when assigning its value, only assigning a TSharedPtr < AActor > instead of an AActor*.

And yes, the pointer to a pointer is out of both necessity and performance.

What am I to do here? How can I achieve this?

Hi,

UE4’s reflection system doesn’t support pointers-to-pointers. The reflection system cannot help you in what you are trying to achieve.

If you really need to point to pointers, then you need to manually manage that relationship. i.e. when the actor pointer gets destroyed, you need to know about and clean up any pointers which you know are pointing to it. This is just what you would need to do in non-reflected C++.

Alternatively, you need to introduce an intermediate UObject to manage those pointers:

UCLASS()
class UInnerPointer : public UObject
{
    GENERATED_BODY()

public:
    UPROPERTY()
    AActor* InnerPtr;
};

UCLASS()
class UYourClass : public UObject
{
    GENERATED_BODY()

public:
    UPROPERTY()
    TArray<UInnerPointer*> PointersToPointers;
};

So it’s not really pointers to actor pointers, but pointers to objects which contains actor pointers, and both levels of pointers are properties. This would mean a whole bunch of extra objects to create.

Given that this would be quite a departure from your current setup and that you’ve said that you’ve ended up with this architecture due to performance concerns, I would recommend doing profiles to see how much it ends up costing you.

Hope this helps,

Steve

Seems to work in 4.14, was an issue in my code.