# Struct destroy and garbage collection

**URL:** <https://forums.unrealengine.com/t/struct-destroy-and-garbage-collection/1302783>\
**Category:** C++\
**Tags:** Destroy, Struct, garbage-collection, unreal-engine, question, CPP\
**Created:** [September 19, 2023, 5:02am UTC](https://forums.unrealengine.com/t/struct-destroy-and-garbage-collection/1302783 "2023-09-19T05:02:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [September 19, 2023, 5:02am UTC](https://forums.unrealengine.com/t/struct-destroy-and-garbage-collection/1302783/1 "2023-09-19T05:02:26Z")

</div>

Hello.

I wanted to know if there was a way to destroy or delete structs from memory, and how unreal engine performs garbage collection for structs.

Do structs that have not been used, get purged from memory?

For example, I wish to make an ability system where I have structs for health, mana, stamina as:

```auto
struct FAttributeStruct {
    float value;
    float minimum_value;
    float maximum_value;
};

```

If I have values for health struct-variable in a character/Actor/ActorComponent, will the health struct be deleted from memory if not used, by the UE garbage collector?

Secondly, In this scenario, can I also make a `TMap<FString, FAttributeStruct *>`, such that I can map keys to `FAttributeStruct` pointers?  
Will there be a case where dereferencing those pointers can yield a `nullptr`?

Thirdly, can I destroy structs similar to destroying actors or components?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [September 19, 2023, 12:52pm UTC](https://forums.unrealengine.com/t/struct-destroy-and-garbage-collection/1302783/2 "2023-09-19T12:52:20Z")

</div>

The engine does not garbage collect anything other than UObjects. If you create structs (or indeed, any data type) with `new`, you are responsible for deleting them. Even if you allocate a struct within a UObject, you must delete that data before the UObject is destroyed - the engine can’t and won’t do that for you.

Where appropriate, you can and normally should use [Smart Pointers](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/SmartPointerLibrary/) to make this simpler.

---

<div class="post-metadata">

**Author:** ![titan\_nic110](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@titan\_nic110](https://forums.unrealengine.com/u/titan_nic110)\
**Post date:** [September 19, 2023, 1:01pm UTC](https://forums.unrealengine.com/t/struct-destroy-and-garbage-collection/1302783/3 "2023-09-19T13:01:52Z")

</div>

it’s depend on how you create your sturct, if not use keyword “new”or"malloc",then usually you don’t need to worry about garabage.
