# Do delegates automatically get removed when an actor is destroyed?

**URL:** <https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [October 25, 2020, 9:00pm UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479 "2020-10-25T21:00:30Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![BitRot](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bitrot/32/498776_2.png) [@BitRot](https://forums.unrealengine.com/u/BitRot)\
**Post date:** [August 16, 2021, 11:19am UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/5 "2021-08-16T11:19:01Z")

</div>

For implementation details on delegates, see `Engine\Source\Runtime\Core\Public\UObject\ScriptDelegates.h`.

Multicast dynamic delegates store an array of script delegates. The concern with not calling RemoveDynamic is that we’ll be adding things to this array and never removing them.

If we look at the definitions for AddDynamic, AddUniqueDynamic, and RemoveDynamic, we see that we call the function `CompactInvocationList()` before each one, which is defined as follows:

```
void CompactInvocationList() const
	{
		InvocationList.RemoveAllSwap([](const TScriptDelegate<TWeakPtr>& Delegate){
			return Delegate.IsCompactable();
		});
	}

```

Delegate.IsCompactable() is defined as:

```
inline bool IsCompactable() const
	{
		return FunctionName == NAME_None || !Object.Get(true);
	}

```

Basically, every time we make any changes to the list of delegates, we delete any delegates that reference a listener that’s been destroyed.

**So, in answer to your question** :

Yes if you add or remove delegates after the actor is destroyed. No if you don’t.

Or in other words, usually yes.

In some probably very rare cases you may want to manually manage delegates, and tight memory management is probably good practice.

---

_[View the full topic](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479)._
