Protect TArray<TArray<AActor *>> from Garbage Collector

Hi I have a class that has a

TArray<TArray<ACraftBox* >> CraftBoxes;

as one of its members.
Every time after a little bit of time running my game always crashes when I try to Construct the Widget used for its display.

I think it may be due to the fact that the Garbage Collector clears the actors in the Array.

Usually you need to flag it as UPROPERTY() but since TArray<TArray<>> is not supported by Blueprints I can’t be sure that it isn’t collected by GC.

Do you know any alternatives please ?
Thanks

There’s likely a better way, but if all else fails, you can set a UObject to manually GC by calling Object->AddToRoot(). Keep in mind, this means that you need to manually Object->RemoveFromRoot() when you’re no longer using the object, and if you’re in PIE, you need to hook FEditorDelegates::OnPrePIEEnded and call Object->RemoveFromRoot() and Object->FinishDestroy().

Wrap your inner TArray in a struct, to make it supported by UPROPERTY

USTRUCT()
struct FCraftBoxes
{
    GENERATED_BODY();
    UPROPERTY()
    TArray<ACraftBox*> Boxes;
};

UCLASS()
class ...
{
    UPROPERTY()
    TArray<FCraftBoxes> CraftBoxes;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.