Deleting referencing objects: how does that work with garbage collection?

How does garbage collection and uproperty system handle Destroy() method?

Let’s say I have

UPROPERTY() 
USomething *something;

That points at some UObject-based class. Component, actor, anything.

Then in my code, something else calls Destroy() method of “something” object.

What happens in that scenario?

As far as I can tell, the system simply marks something as “pending for kill” and doesn’t do anything else.
Is this how it is supposed to work? I sorta would expect system to kill the object, then set all existing UProperty pointers to it to nullptr. Speaking of which, is it a good idea to check for object’s validity with:

if (x && !x->isPendingKill())

?

GC does zero IsPendingKill() references. Set gc.CollectGarbageEveryFrame true and you’ll see it happen…a lot. Not recommended as this is a perf killer

Definitely call IsPendingKill() for objects that are dead but don’t know it yet :slight_smile:

Thanks for the info. How frequently does GC collect objects by default?

The reason I’m interested in the topic, is because I have projectiles with particle systems attached to them, and I needed to delay destruction of the actual projectile actor so the smoke trail properly disappears.

The thing is, if I set particle system component to selfdestruct (after disabling emission) and then periodically check it has been zeroed by GC, apparently object sits in the scene for quite a while and checking for isPendingKill seems to be a better option.

Can you not use the OnSystemFinished delegate?

I’m not one hundred percent sure if it is triggered when i suppress emission or deactivate the system, and I don’t know in advance if there are particle systems attached to the object at all.

I had issues making inherited particle system component work, so I opted out for base class that doesn’t have particle system component but checks if there are any of them attached, and waits if particle system components are present.