# Garbage Collect UStructs()?

**URL:** <https://forums.unrealengine.com/t/garbage-collect-ustructs/275350>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350 "2014-03-11T02:13:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350/1 "2014-03-11T02:13:27Z")

</div>

Dear Friends at Epic,

**Thank you so much for UE4! Yay!**

I am storing TArrays of UStructs().

New UStructs() are created locally inside a function called from DrawHUD and have no references to them after the function completes except the dynamic array they are stored in.

I periodically **.Empty()** the array and then refill it with new UStructs, and the old ones are no longer useful to my game.

**Is there a way I can explicity delete the old UStructs() ?**

Here’s my current UStruct definition:

```
USTRUCT()
struct FButtonStruct
{
	GENERATED_USTRUCT_BODY()

	//Vars
	int32 type;
	int32 arrayIndex;
	float minX;
	float maxX;
	float minY;
	float maxY;
	
	// ~~~~~~~~~~~~~~~~~~~
	
	//default properties
	
	FButtonStruct()
	{
		type = 255;
		arrayIndex = -1;
	}
};

```

Here’s my Dynamic Array:

```
TArray FileBrowserButtons;

```

Thanks!

Rama

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350/2 "2014-03-11T02:13:28Z")

</div>

It’s probably a good idea to initialize all members of your struct in the constructor. If you are adding them to the array by first allocating on the stack, for example, you will have bogus data in non-init’d members.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350/3 "2014-03-11T02:13:28Z")

</div>

Thanks for the info James!

🙂

The garbage collection will occur automatically for structs with no references once removed from the dynamic array?

🙂

Rama

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350/4 "2014-03-11T02:13:29Z")

</div>

That is not an array of references to structs, it is an array of actual structs, so you don’t need to worry about GC. Our GC _only_ collects UObjects though, you are responsible for managing non-UObject lifetimes yourself if you allocate them outside something like an array inside a UObject.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 2:13am UTC](https://forums.unrealengine.com/t/garbage-collect-ustructs/275350/5 "2014-03-11T02:13:29Z")

</div>

Thanks for the info James! 🙂 I’m having a lot of fun with UE4 c++, and I am making substantial progress with my project! 🙂 Thanks for your help!
