Is there a way to flag a UObject to never be garbage collected

Dear Friends at Epic,

I know this is a bit of an off-the-wall question, but it would help my debugging :slight_smile:

I am running into a situation where I am working with code that creates a new object at runtime extended from the base class of UObject directly.

This class is created using NewObject during runtime, in the following manner:

TheOBJ = NewObject();

There are references created to this object in my player controller class as well as a another runtime-spawned actor class.


An average of 45 seconds to 2 minutes into the game time, after object is first created, it becomes null.

Causing my game to crash

I have tested this consistently for the past…15 hours :slight_smile:

Took me a while to be able to present this info to you as the source of the crash


So I know that somehow the object is ceasing to exist.

I can only assume it is being garbage collected.

Is there a way to flag an object to never be garbage collected?


By the way

I am in no way asserting that (ahem, oh my, so many assertions that didnt make it in past 15 hours)

the garbage collection system is at fault here,

I am just wanting to add another bit of info to my understanding about the situation by absolutely verifying that the obj is not being GC’ed

:slight_smile:

Rama

Please don’t edit the titles of your questions unless it is to correct something. Adding tags or things like “problem solved ty Marc Audy” just make it hard to read.

I guess I will just continue the use of my personalized pic of happy approval

tyepicdevsheart.jpg

An object can be added to the root set using AddToRoot. All objects in the root are considered referenced and will not be garbage collected.

That said, is TheOBJ a class variable or what is the reference held to it? If it is a class variable it must be a UPROPERTY or else the garbage collection system won’t be aware of it.

Thanks Alexander and Marc!

Marc, AddToRoot was exactly what I was looking for! Thanks again!

TheOBJ is indeed a class variable, and it is amazingly big and important news for me to know that such references must have UPROPERTY or GC wont be aware of it

Thanks again Marc!

Rama

I just wanted to follow up on this and say

t

h

a

n

k

y

o

u

Marc Audy!

You are a hero!

You just helped me solve a 24+ hour problem!

It was indeed the Garbage Collection system, because I was not using AddToRoot or using UPROPERTY() references

Woohoooo!

Thanks Marc!

Rama

Hey Rama,

If you want the UObject to not be collected, you will have to maintain a reference to it. Basically, if your reference to an object falls out of scope and there are no references left, it will be collected. So be sure to maintain a reference to anything you do not want to be deleted. For a more detailed explanation of garbage collection, please see this reference: Garbage collection (computer science) - Wikipedia

Thank you,

Alexander

I agree Alex, the interesting and very important point that Marc brought up is that if the reference to the object is a class variable, " If it is a class variable it must be a UPROPERTY or else the garbage collection system won’t be aware of it "

That was really good to know!

:slight_smile:

Rama