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