Keep in mind for pointers to UObject types that you must store them under UPROPERTY to prevent them from being garbage collected while they are still in use. If not the garbage collector is going to run over them regardless.
The normal process is:
- Have a soft pointer on a UPROPERTY.
- Load the data from the soft ptr on demand, or store the loaded instance under UPROPERTY.
- When done with the instance, destroy it.
- pointers to destroyed UObject are automatically nulled.
- Unreferenced data is collected.
Unreferenced means “not in UPROPERTY, not in scope”, so if you were to store an instance on a class property not marked UPROPERTY the GC will collect it too soon. You can think of the referencing process as one large tree from base to branches. Everything is referenced by something else, and if it’s not the branch falls off and is collected as garbage. UPROPERTY is used to build the references, the GC just runs every so many seconds to check what fell off.
It’s probably easiest to just load things on demand from the soft ptr in some function when you need it. The thing with storing hard pointers under UPROPERTY is that the classes just don’t get unloaded. People often run into this when they realize their class is recursively loading a ton of other classes into memory before changing pointers to soft. Say, datatables not using soft ptrs for example. massive memory waste, everything loads right away and often never unloads.