About UObject dynamically create and delete

Hi guys,

I have a question about new a UObject (using ConstructObject). If the UObject variable does not use UPROPERTY or it is a UObject pointer in a function, after I “ConstructObject” the UObject. will it be GC automatically or it will in memory I delete it manually?

Also, When I load resource such as static mesh, texture or blueprint from hard drive using something like “StaticLoadObject”, How to maintain the lifetime of these loaded UObjects (may be UBlueprint, UClass, UStaticMesh)? How to keep them available? Will it be GC or do I have to delete it explicitly?

In general any time you have a UObject pointer, be it something you created via ConstructObject or loaded with StaticLoadObject you need to have a UPROPERTY which references it in an object that is itself referenced by something that eventually reaches the root (for example Actor is referenced by Level which is referenced by World which is a root object).

If you don’t have such a reference the next time garbage collection runs it will be identified as unreferenced and deleted. You never need to call delete yourself manually, though you can call MarkPendingKill() on an object if you want to force it to be garbage collected next pass regardless of whether it is still referenced by a property.