Search Unity

Possible Memory Leak or Issue With Unity Event Implementation/Usage

Discussion in 'Scripting' started by vikasrawat, Aug 17, 2021.

  1. vikasrawat

    vikasrawat

    Joined:
    May 24, 2017
    Posts:
    2
    I'm facing this issue for the following use case of using Unity Event :
    1. Have a reference of Unity Event Object.
    2. Add any number of listeners to it.
    3. Invoke the event once.
    4. 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.
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    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.)