How to garbage collect USTRUCTs?

I am having a hard time understanding how to delete UStructs. Unlike UObjects, they are not handled by Unreal’s garbage collection system (is this correct?) and we are supposed to delete all dynamically created UStructs manually. However, there are some occasions when I can’t think of the right time to delete them. Consider the damage code:

float AMyCharacter::TakeDamage(
	float DamageAmount,
struct FDamageEvent const& DamageEvent, ... )

Here, FDamageEvent, which is an UStruct, is passed by reference, so the object which created it is responsible for deleting it. But how long will it stay there? For example, I might want to store an array of UStruct pointers for damage over time mechanics, or maybe it’s better to just copy the data and never use pointers.
What will happen if I use shared pointers here?

TSharedRef<FEternalDOTEvent> DmgEvRef(&DamageEvent);

And if I pass UStruct* from Blueprints, will these ever get GC’ed? Sorry for having done little research on this, but I really need to get it working without crashing or memory leaks.

Hi fed0r,

You’re right that everything derived from UObject is managed by GC, everything else is not. Time a struct stays alive really depends on the owner of the struct. usually structs (not pointers to structs) are just memebers of the owning class in which case they will stay alive as long as the owner stays alive. I’d say the best way to make sure something like this is alive is to keep a local copy of the struct.

1 Like