# 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:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skoutyboy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/skoutyboy/32/876676_2.png) [@Skoutyboy](https://forums.unrealengine.com/u/Skoutyboy)\
**Post date:** [October 25, 2020, 9:00pm UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/1 "2020-10-25T21:00:30Z")

</div>

I try to call .RemoveDynamic() in the destructor and it is crashing my program sometimes…

I am calling OnComponentBeginOverlap.AddDynamic(this, &ThisActor::Function).

The class in the parenthesis gets destroyed and I call OnComponentBeginOverlap.AddDynamic() on a new class that is spawned.

I don’t want the component that is calling the delegate function to have a bunch of useless delegates on it.

---

<div class="post-metadata">

**Author:** ![King\_Crazy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@King\_Crazy](https://forums.unrealengine.com/u/King_Crazy)\
**Post date:** [October 26, 2020, 12:55am UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/2 "2020-10-26T00:55:22Z")

</div>

Instead of handling **AddDynamic** and **RemoveDynamic** in the constructor/destructor, have you tried handling them in the **BeginPlay()** and **EndPlay()** functions? If you want to be safe and ensure that the delegates are bound as soon as possible, you could call **Super::BeginPlay()** and **Super::EndPlay()** _after_ the delegates are added/removed.

To answer the title question, I do not believe so. I think all delegates must manually be removed. Ideally **EndPlay()** seems like the best place to handle it, from my experience (That said, UE4-built-in delegates might handle their cleanup differently).

---

<div class="post-metadata">

**Author:** ![Skoutyboy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/skoutyboy/32/876676_2.png) [@Skoutyboy](https://forums.unrealengine.com/u/Skoutyboy)\
**Post date:** [November 4, 2020, 5:46pm UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/3 "2020-11-04T17:46:39Z")

</div>

I forgot that Endplay() was a thing! This is perfect, thank you!!!

---

<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:14am UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/4 "2021-08-16T11:14:32Z")

</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.

---

<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.

---

<div class="post-metadata">

**Author:** ![Skoutyboy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/skoutyboy/32/876676_2.png) [@Skoutyboy](https://forums.unrealengine.com/u/Skoutyboy)\
**Post date:** [August 16, 2021, 6:21pm UTC](https://forums.unrealengine.com/t/do-delegates-automatically-get-removed-when-an-actor-is-destroyed/473479/6 "2021-08-16T18:21:51Z")

</div>

Awesome! Thank you so much for the detailed answer!!
