UObjects in use destroyed by GC

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

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.

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.

2 Likes

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)

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

1 Like