Download

Memory management of a redefined UObject ( UPROPERTY )

What happens to the memory in the case below? And what would be the best way to deal with this?

.h


UPROPERTY()
UObject* MyObject;

.cpp


MyObject = NewObject<UNormalObject>();

**// some time later...**
**// What happens to the previous content of MyObject? Is it deleted?**
MyObject = NewObject<USpecialObject>();

I’m sorry if this question is a duplicate, I haven’t found similar posts.
Thank you!

It’s a memory leak.

That’s actually fine. Every time a UObject is created, it’s registered with the GC system. If no class is holding on to that reference (via a UPROPERTY), then the object is deleted during GC.

If you want to “force” it to be cleaned up ASAP, you can simply call “MyObject->MarkPendingKill()” before you toss the reference away.

That was my concern @Emaer !
Thank you very much @ExtraLifeMatt !!

That’s why I hate GC. The obvious things are not obvious any more.