Help with struct garbage collection

Hi, I’d just like a bit of clarification when using structs in my project. In the documentation I’ve read;

Unlike a UObject , UStruct instances are not garbage collected. If you create dynamic instances of them, you must manage their lifecycle yourself.

What does this mean? I’ve followed tutorials on the Unreal Learning Portal which used structs and there was no mention of managing their life cycles. I’ve used structs extensively through my project and not a had a problem so far, I was just curious in case I am doing something wrong.

Thanks!

It basically means if you have new TheStructType(...); anywhere in your code you have to make sure that allocated memory gets deleted eventually (e.g. via smart pointer or other mechanism). If you only use TheStructType in local variables, function parameters or as members in your classes/structs without dynamically allocating which is the most common and typically how they are used in UE then there is no need to managing their life cycles yourself as there are other mechanism that already deal with it for you. E.g. the UObjects that contain these structs as members will be managed by the garbage collector and local variables/function parameters created on the stack are automatically destroyed again.

1 Like

That make sense, thanks for the clarification