Where and How is the new FStrcut deleted?

Hello there,


In AbilitySystemComponent.h, the MakeOutgoingSpec() function declares a new FGameplayEffectSpec. I’m wondering where and how is that new FGameplayEffectSpec is destructed? Could anyone please tell me about that?

Hey Andrew,

You should not create instances of struct types. They will not be monitored and you will have to delete them manually. Besides, it might be pretty expensive if you create many instances on your own on a frame basis. To work around that, create a simple value of your struct type and pass-by-value it to other functions or constructors if you are going out of the scope of the struct variable (just like in the code you sent).
If you really need to create instances and maintain them, you have to make it a UClass and create it via NewObject(). This way, it’ll be monitored by the garbage collection and will be destroyed when there are no references to your instance. Don’t forget to use smart pointers when creating new instances of your class.

Hi Sami,
thanks for replying :smile:
I am trying to get used to UClass and use NewObject, since with them I know I don’t need to worry about deleting them manually.
I am learning the Gameplay Ability System and I found some “official” codes uses “new” for new struct variables, and in the image it is an example.
We “shoud not” “new” an class instance, and similarly we also shouldn’t “new” a struct. This is what I am intuitively aware of. So when I see the GAS use it I am very confused. (It doesn’t use “new” a lot for structs, but I do see them in Gameplay Effect Context and Gameplay Target Data. So I am quite confused. :persevere:

1 Like

Hi Sami,
I just found it out.
The GAS does use new on structs, but in their context every “new” struct is wrapped with a (eg) Shared Ptr, then it is managed by that smart pointer.

1 Like