Sunday, June 24, 2012

C# Delegates, Destructors, and Garbage Collection

Just this past week, I've encountered a very specific memory leak in Unity 3D that could happen just generally coding in C# as well. Basically the leak involves subscribing to a delegate in a class' Constructor and then unsubscribing to that delegate in the class's Deconstructor. Here's the gist of what I was trying to do.

I thought this class -- which did not derive from MonoBehaviour -- would act like MonoBehaviours and call its destructor when a scene was changed. That was not the case at all. When I ran the program, the SNController objects would stick around and the loggedIn and loginFailed functions would be called multiple times as I exited and entered scenes.

It turns out that -- because C# is a managed language -- destructors for objects are called when the objects are cleaned up by the Garbage Collector(GC).

So why didn't the GC destroy the SNController object? The problem was that the GC saw that this object was still subscribed to delegates from the FacebookManager class and would not destroy the object. Therefore, the Destructor would never be called, creating a Catch 22 situation.

I needed to unsubscribe from these delegates explicitly when I was done with the SNController object. To do that, I wrote a Dispose method.

Since SNController was instantiated as a member of a MonoBehaviour object, I would call SNController's Dispose method in the MonoBehaviour's OnDestroy method. This eliminated any extraneous references from the SNController object at the end of the scene and GC would destroy the object.