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.

10 comments:

rmouniak said...

It 's an amazing article and useful for developers
.Net Online Training Bangalore

ragul ragul said...

Read all the information that i've given in above article. It'll give u the whole idea about it.

Microsoft azure training in Bangalore


Power bi-training in Chennai

sasitamil said...

This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Mounika said...

Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!

Python Online certification training
python Training institute in Chennai
Python training institute in Bangalore

Softgen Infotech said...

Such a great information for blogger iam a professional blogger thanks…

Softgen Infotech is the Best SAP S4 HANA Training in Bangalore located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.

aarthi said...

Such an awesome article.Very nice.Keep going.
Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

Jayalakshmi said...

I am feeling great to read this.you gave a nice information for us.
please update more.
oracle training in chennai

oracle training in tambaram

oracle dba training in chennai

oracle dba training in tambaram

ccna training in chennai

ccna training in tambaram

seo training in chennai

seo training in tambaram

jeni said...

It¦s truly a nice and helpful piece of info. I am satisfied that you simply shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
oracle training in chennai

oracle training in velachery

oracle dba training in chennai

oracle dba training in velachery

ccna training in chennai

ccna training in velachery

seo training in chennai

seo training in velachery

shiny said...

Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...



oracle training in chennai

oracle training in annanagar

oracle dba training in chennai

oracle dba training in annanagar

ccna training in chennai

ccna training in annanagar

seo training in chennai

seo training in annanagar

deiva said...

Thanks for sharing the knowledgeable stuff to enlighten us no words for this amazing blog.. learnt so many things I recommend everyone to learn something from this blogger and blog.. I am sharing it with others also..
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr