Explicitely Delete a UObject

Hello,

UObjects are managed by the garbage collector. To create a UObject appropriately, use NewObject(), NewNamedObject() and ConstructObject(). It is possible to configure the way UObjects will be handled by garbage collector at the time of creation with Object Flags enumeration. (If you like to learn more about UObject Instance Creation, you can go here:
Creating Objects in Unreal Engine | Unreal Engine 5.1 Documentation ) This way, you should not call new or delete on UObjects.
If UObject is no longer needed, it usually means that there are no references to it (this may, however differ, depending on the context and garbage collection flags used at the moment of UObject creation). In this situation, you can run ForceGarbageCollection() function:

GetWorld()->ForceGarbageCollection(true);

Please note, that calling this method may cause crashes in some situations, particularly when object is already being destroyed by garbage collector or has a value of null.

Also, if you like to learn more about Unreal Object Handling, you can go here:

Hope this helped!
Good luck!

4 Likes