We're developing a large scale Unity3D-based video game and we've become concerned that a major memory leak is occurring due to some bad event subscriber disposal somewhere. Unfortunately we haven't been able to attach any of the popular C# profilers to determine the root cause of the memory leak.
We thought one quick way to determine if there is a loose end in our event system is to write a quick method we could run during runtime to export all the Action properties in all of our objects with a count of the current subscribers.
The hope here is that we quickly see some huge numbers in a couple of events and can quickly identify the missing -= unsubscribe.
The question I have here is: Is there a way to iterate over all our objects to collect all event handlers and count their subscribers?
I've made a quick start on iterating over our units. However I'd love a type-generic way to do it as we have a lot of derived types in our game.
public static object ExportAllEvents(params string[] args)
{
foreach (var wUnit in GameData.LEVEL.getUnits())
{
foreach (var eventInfo in wUnit.GetType().GetEvents())
{
// Hrmmm...
}
}
// TODO: CSV export here
return "Success";
}