Possible Memory Leak or Issue With Unity Event Implementation/Usage (852348)

I’m facing this issue for the following use case of using Unity Event :

  • Have a reference of Unity Event Object.
  • Add any number of listeners to it.
  • Invoke the event once.
  • Now Remove each event listener one by one.

Now in above scenario , until I release the instance of the event listener , the listener basically holds a reference to the target of each listener, even though I have removed the listeners. I could trace this to how the copy of listener targets are maintained inside ‘m_executingCalls’ list internally.

Now is there something wrong by the way I’m trying to use Unity Events or is there something else I’m missing. Seems like a fairly usual way in which someone would use the API.

Thanks For Reading.

Yeah, looks like m_executingCalls is only cleared/set before invoking the event, not after it.

You could submit a bug report, as this really isn’t ideal behavior.

As a workaround, you could try to invoke the event again after clearing it, this should update and clear m_executingCalls. (You can also use RemoveAllListeners instead of removing each listener one by one.)