# Garbage collector removes elements from TArray of UStruct

**URL:** <https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 31, 2017, 11:33am UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944 "2017-03-31T11:33:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [March 31, 2017, 11:33am UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/1 "2017-03-31T11:33:05Z")

</div>

I made 2 simple struct using the USTRUCT() macro. One basically holds an array of the other; that’s because I wanted to make a 2-dimensional array of struct and Unreal wouldn’t let me:

```auto

USTRUCT(BlueprintType)
struct FAssemblyMove
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Assembly Move Struct")
    int32 MoveNumber;

    FAssemblyMove(int32 pMoveNumber)
        : MoveNumber(pMoveNumber)
    {

    }
};

USTRUCT(BlueprintType)
struct FStep
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Assembly Step Struct")
    TArray<FAssemblyMove> MoveList;

    FStep()
    {

    }
};

```

Now, in the class file in which these structs are declared I have this (an array and a method to add an element to it):

```auto

UCLASS()
class FPSTEST2_API UAssemblyInfoManager : public UObject
{
    GENERATED_UCLASS_BODY()
public:
    FAssemblyMove AddMove();
private:
    UPROPERTY()
    TArray<FStep> mStepsList;
};

```

The code that adds the element is pretty straightforward:

```auto

FAssemblyMove UAssemblyInfoManager::AddMove()
{
    // I retrieve the last element of the array
    FStep step = mStepsList[mStepsList.Num() - 1];

    FAssemblyMove newMove;
    newMove.MoveNumber = step.MoveList.Num() + 1;

    step.MoveList.Insert(newMove, step.MoveList.Num());

    return newMove;
}

```

Now, as a test, I add an element to “mStepsList”; then I call AddMove() to add one element to the inner array that it contains. This seems to work, but “soon” (not sure when) the MoveList array contained inside the FStep I retrieve from mStepsList gets deleted. I tried several times, but every element I add always get deleted soon.

I’m thinking is a garbage collection problem, but as far as I know I’m playing it “safe” by using UPROPERTY() on all these variables. What am I doing wrong then?

---

<div class="post-metadata">

**Author:** ![Pierdek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pierdek](https://forums.unrealengine.com/u/Pierdek)\
**Post date:** [March 31, 2017, 1:00pm UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/2 "2017-03-31T13:00:23Z")

</div>

Weird, does UAssemblyInfoManager is referenced by something with UPROPERTY field ?

---

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [March 31, 2017, 1:37pm UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/3 "2017-03-31T13:37:02Z")

</div>

> [@Pierdek;688749](#):
>
> Weird, does UAssemblyInfoManager is referenced by something with UPROPERTY field ?

Yes it is. Also, the object that uses UAssemblyInfoManager has also the UPROPERTY() field, and so does every other object “in the path” from the player pawn class to the code I posted above. I really don’t get what is wrong…

EDIT: one other thing is worth mentioning. When I add a FStep to the mStepsList array using this piece of code (I didn’t show it because I didn’t think it was relevant)

```auto

void UAssemblyInfoManager::AddStepAtIndex(int32 pStepIndex)
{
    FStep newStep;
    mStepsList.Insert(newStep, pStepIndex);
}

```

it works as expected, meaning that the FStep I create and add here isn’t garbage collected.

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [March 31, 2017, 4:49pm UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/4 "2017-03-31T16:49:39Z")

</div>

I would think the struct in this case isn’t GC’d due to the fact that you aren’t dynamically allocating it, it’s just a simple pass by value. More likely, I think your UAssemblyInfoManager is getting cleaned up somewhere along the way and you just aren’t noticing it till you try to do some action upon it (i.e. add a value to that array member).

A quick way to test this is just to create a simple destructor for both the class and the struct and toss a break point in there - see what is getting hit when.

---

<div class="post-metadata">

**Author:** ![The\_Beej](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@The\_Beej](https://forums.unrealengine.com/u/The_Beej)\
**Post date:** [April 1, 2017, 6:19am UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/5 "2017-04-01T06:19:50Z")

</div>

```auto

FStep step = mStepsList[mStepsList.Num() - 1];

```

Needs to be

```auto

FStep &step = mStepsList[mStepsList.Num() - 1];

```

You’re making a new copy of the FStep object and modifying the copy, so your items were never added in the first place.

And also, GC only ever operates on UObjects, structs will never be GC’d and need to be manually managed.

---

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [April 1, 2017, 8:43am UTC](https://forums.unrealengine.com/t/garbage-collector-removes-elements-from-tarray-of-ustruct/88944/6 "2017-04-01T08:43:02Z")

</div>

> [@The Beej;689097](#):
>
> ```auto
> 
> FStep step = mStepsList[mStepsList.Num() - 1];
> 
> ```
> 
> Needs to be
> 
> ```auto
> 
> FStep &step = mStepsList[mStepsList.Num() - 1];
> 
> ```
> 
> You’re making a new copy of the FStep object and modifying the copy, so your items were never added in the first place.
> 
> And also, GC only ever operates on UObjects, structs will never be GC’d and need to be manually managed.

Man, you’re right! So stupid! I don’t know how I missed that.

Thank you so much! Also thanks to anyone who tried to help me 🙂
