# UObjects in use destroyed by GC

**URL:** <https://forums.unrealengine.com/t/uobjects-in-use-destroyed-by-gc/472354>\
**Category:** Editor Scripting\
**Tags:** garbage-collector, Destroy, reference, unreal-engine, question\
**Created:** [October 3, 2020, 4:52pm UTC](https://forums.unrealengine.com/t/uobjects-in-use-destroyed-by-gc/472354 "2020-10-03T16:52:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISeeIcySea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ISeeIcySea](https://forums.unrealengine.com/u/ISeeIcySea)\
**Post date:** [October 3, 2020, 4:52pm UTC](https://forums.unrealengine.com/t/uobjects-in-use-destroyed-by-gc/472354/1 "2020-10-03T16:52:37Z")

</div>

Hello,

I have a TArray of pointers to UObjects in a custom class, that I create at runtime using NewObject().

I am performing an action on those UObjects on tick, but I’ve noticed that after 60s, the garbage collector destroys them, even though the documentation says that in order for objects not to be destroyed, you should mark them with UPROPERTY() or store them in a TArray (or similar). [link to documentation](https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Objects/Optimizations/index.html#garbagecollection)

I am doing exactly that, but the GC still destroys my objects and I can’t figure out why. I even tried storing them as `TSharedRef<UObject>`, but that didn’t help either.

Does anyone know what I could do about this?

Thank you.

---

<div class="post-metadata">

**Author:** ![ISeeIcySea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ISeeIcySea](https://forums.unrealengine.com/u/ISeeIcySea)\
**Post date:** [October 4, 2020, 11:52am UTC](https://forums.unrealengine.com/t/uobjects-in-use-destroyed-by-gc/472354/2 "2020-10-04T11:52:21Z")

</div>

I think I found a solution, even though I don’t like it very much. You need to add UPROPERTY() to your container so that the GC doesn’t delete its contents straight away.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [October 4, 2020, 7:17pm UTC](https://forums.unrealengine.com/t/uobjects-in-use-destroyed-by-gc/472354/3 "2020-10-04T19:17:53Z")

</div>

Hi! In fact UPROPERTY meta is one of the few things, that save UObject pointers from collecting. Objects without that tag can be consiedered as unprotected pointers. Others ways to be safe from collecting are more special:

1. AddToRoot ([UObjectBaseUtility::AddToRoot | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/API/Runtime/CoreUObject/UObject/UObjectBaseUtility/AddToRoot/index.html))

2. Also you can look at this  
[https://answers.unrealengine.com/questions/493997/best-way-to-avoid-uobjects-garbage-collection.html?sort=oldest](https://answers.unrealengine.com/questions/493997/best-way-to-avoid-uobjects-garbage-collection.html?sort=oldest)
