Skip to content

Instantly share code, notes, and snippets.

@lonewolfwilliams
Last active August 1, 2016 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonewolfwilliams/f4bd304e7c9d974cb04774ffba14685c to your computer and use it in GitHub Desktop.
Save lonewolfwilliams/f4bd304e7c9d974cb04774ffba14685c to your computer and use it in GitHub Desktop.
track event delegate registration, to spot unbalanced addition and removal...
//sometimes, when it's not your codebase and you have a lot of events flying round,
//it's useful to spot where an event may have been registered twice on the same component,
//or added and not removed (reference retained outside lifecylce of GameObject)
//this expansion of the event property may help...
//note: for whatever reason target may be null...
//pro tip: use the console to backtrack to the source of the registration
//
//idea adapted from juval Lowy: Programming .Net Components (O' Reilly)
private event EventHandler m_onClick;
public event EventHandler onClick
{
add
{
m_onClick += value;
Debug.LogFormat("adding delegate {0} to GO {1} now has {2} delegates", (value as Delegate).Target, GetInstanceID(), m_onClick.GetInvocationList().Length);
}
remove
{
Debug.LogFormat("removing delegate {0} to GO {1} now has {2} delegates", (value as Delegate).Target, GetInstanceID(), m_onClick.GetInvocationList().Length);
m_onClick -= value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment