# Is it necessary to unbind events before destroying an actor

**URL:** <https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 7, 2017, 9:19pm UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816 "2017-06-07T21:19:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f53ee2a91](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f53ee2a91](https://forums.unrealengine.com/u/anonymous_user_f53ee2a91)\
**Post date:** [June 7, 2017, 9:19pm UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/1 "2017-06-07T21:19:41Z")

</div>

I have some custom events bound in an AIController, and I was wondering if it is necessary to unbind those events before calling DestroyActor in order for it to be garbage collected. If it is not necessary, do the events unbind immediately, or only when the controller is garbage collected?

Editing for clarification:

- Do event bindings prevent garbage collection
- If not, do they get removed when the actor is flagged for collection, or upon being collected

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a9f83bc6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a9f83bc6](https://forums.unrealengine.com/u/anonymous_user_a9f83bc6)\
**Post date:** [June 8, 2017, 8:28am UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/2 "2017-06-08T08:28:27Z")

</div>

Yes it is recommended to unbind events when you no longer need them, they can be fired unpredictably and cause bugs very hard to track down.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f53ee2a91](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f53ee2a91](https://forums.unrealengine.com/u/anonymous_user_f53ee2a91)\
**Post date:** [June 8, 2017, 6:26pm UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/3 "2017-06-08T18:26:19Z")

</div>

Best practice, sure. But that doesn’t really address my question. I have run into some other issue regarding destroying the AIController, and I’m doing things a different way now, but it would still be useful to know if event bindings prevent garbage collection (i.e., is unbinding _necessary_) and, if not, if they get removed when it gets flagged for collection, or upon being collected.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a9f83bc6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a9f83bc6](https://forums.unrealengine.com/u/anonymous_user_a9f83bc6)\
**Post date:** [June 9, 2017, 11:10am UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/4 "2017-06-09T11:10:38Z")

</div>

No they won’t, the references are weak.  
The problem is that you don’t know if an object is removed yet and you can call accidentally it.

---

<div class="post-metadata">

**Author:** ![EMzuki](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EMzuki](https://forums.unrealengine.com/u/EMzuki)\
**Post date:** [May 17, 2023, 11:35am UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/5 "2023-05-17T11:35:41Z")

</div>

I would also like clarification for this as the question has not really been resolved.

Specifically:

- Do event bindings prevent garbage collection when an actor gets destroyed

---

<div class="post-metadata">

**Author:** ![mcd8604](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mcd8604](https://forums.unrealengine.com/u/mcd8604)\
**Post date:** [May 29, 2023, 10:26pm UTC](https://forums.unrealengine.com/t/is-it-necessary-to-unbind-events-before-destroying-an-actor/395816/6 "2023-05-29T22:26:40Z")

</div>

I’m not an expert on this, so maybe someone can correct me if I’m wrong. My understanding is if you have an AActor (or, generally, a UObject) that a delegate is bound to, it can still be garbage collected. I came to this conclusion after reading [this doc](https://docs.unrealengine.com/5.2/en-US/delegates-and-lamba-functions-in-unreal-engine/#bindingdelegates). Specifically, where it says

> the delegate system can keep a weak reference to the object, so that if the object gets destroyed out from underneath the delegate…

This leads me to believe that the object can be garbage collected while the binding remains. This doesn’t necessarily mean it’s a good thing to leave broken bindings hanging like that.
