#Google Analytic Tracker

Pages

Dec 16, 2009

Event Handler and Memory Leaks

I have been trying to find the time write a post about event handler that causes memory leak. Finally I find the time to write something about.

.NET has Garbage Collect == No Memory Leaks?

Of course NOT. Even in the modern language like C#, we still have to worry about memory leak.

Possible Causes of Memory Leak

  • Failed to close or clean up unmanaged code
  • Created static object that isn’t being use after its first use
  • Failed to dereference event handler in certain situation. (i.e. you did the += handler, but not the –= handler)

Why you need to do –= Event Handler?

This time, I want to explicitly talk about the important of dereference event handler.  It is a common mistake that developers forget to do.

In most cases, you don’t usually need to perform dereference your event handler. For example:

   1: public class Class1
   2: {
   3:     private EventHandler SomeEvent;
   4:  
   5:     public Class1()
   6:     {
   7:         SomeEvent += SomeEventHandler;
   8:     }
   9:  
  10:     private void SomeEventHandler(object sender, EventArgs args)
  11:     {
  12:         // Some something
  13:     }
  14: }

When Class1 is disposed, the entire class is disposed, therefore you don’t need to worry about dereferencing class1’s event handler.

However, if your event handler is attached to an external object, you should dereference your event handler when your class is disposed. For example, the following causes a memory leak:

   1: namespace EventHandlerTest2
   2: {
   3:     public class Program
   4:     {
   5:         public EventHandler SomeEvent;
   6:  
   7:         static void Main(string[] args)
   8:         {
   9:             Program program = new Program();
  10:             for(int i = 0; i < 10; i++)
  11:                 program.Run();
  12:         }
  13:  
  14:         public Program()
  15:         {
  16:             CreateUselessHelper();
  17:         }
  18:  
  19:         public void Run()
  20:         {
  21:             if (SomeEvent != null)
  22:                 SomeEvent(this, null);
  23:         }
  24:  
  25:         private void CreateUselessHelper()
  26:         {
  27:             //Create a Helper class locally, 
  28:             //will this object stick around after this function exit?
  29:             HelperClass helperClass = new HelperClass(this);
  30:         }
  31:     }
  32:  
  33:     public class HelperClass
  34:     {
  35:         public HelperClass(Program program)
  36:         {
  37:             program.SomeEvent += SomeEventHandler;
  38:         }
  39:  
  40:         private void SomeEventHandler(object sender, EventArgs args)
  41:         {
  42:             Console.WriteLine("I am still around!");
  43:         }
  44:     }
  45: }

In the above code, I created a HelperClass object in the CreateUslessHelper() method call. Typically, you would think that once this method is completed, the HelperClass is garbage collected. Unfortunately, in this case, it does not get garbage collected. When the HelperClass.SomeEventHandler attached to Program.SomeEvent, it causes the Program object to reference HelperClass. As a result, the Helper class will stick around until the Program object get garbage collected.

Solution

An appropriate way to clean up this event handler is simply implement IDispoble in the Helper Class. In the dispose method you simply dereference the event handler.

   1: public class HelperClass: IDisposable
   2: {
   3:     private Program _program;
   4:     public HelperClass(Program program)
   5:     {
   6:         _program = program
   7:         _program.SomeEvent += SomeEventHandler;
   8:     }
   9:  
  10:     private void SomeEventHandler(object sender, EventArgs args)
  11:     {
  12:         Console.WriteLine("I am still around!");
  13:     }
  14:  
  15:     public void Dispose()
  16:     {
  17:         _program.SomeEvent -= SomeEventHandler;
  18:     }
  19: }

Here is a example that I wrote which demonstrates the effect of memory leak if event handlers are not dereferenced.

Memory Leak Example (This is my first time trying to use a file hosting service)

Dec 6, 2009

Upgrading From XP to Windows 7 does not require XP Product Key?!?!

After using Windows 7 Ultimate RC for almost half a year, it was about time to install the production version. I had received the Windows7 Professional Upgrade package from BestBuy from my pre-ordered in July. I got it for about $129CND, which is a 50% off promotion.

Since this was an upgrade, I expected Windows 7 installation would require me to install Windows XP or at least has the Windows XP CD installation disk ready.

After using BootCamp to recreate a new partition, I restarted my Mac and began install Windows 7. By the way, I had to clean up the Mac partition, so that BootCamp could split the partition.

Here was the problem I encountered, when I tried to type in my Product Key for Windows 7, it failed to validate it.  After retyping it a couple of times, I gave up and google for solution.

Fortunately, the first entry of my search results gave me the solution, which was to install Windows 7 without the typing in the product key.

http://social.answers.microsoft.com/Forums/en-US/w7install/thread/a7a018fa-17b8-4a27-9657-52ba4d178e87

So, that what I did. I installed it without the product key. Once I am in the desktop, I used Windows Search and open the Windows Activation screen.

I typed in the product key again, and it passed.

Hmm… strange, why it didn’t ask for my Windows XP Key???

After I did a search, it turned out that I may have by pass the procedure of installing Windows XP. Legally speaking, you still need a legit copy of Windows XP, however, with the by pass, it looks like you may not really need your Windows XP or Vista installation disk.

Just a side note, some local computer stores in Calgary or Edmonton are selling Windows 7 Professional OEM 64bit for about $149, which is reasonably inexpensive. If you are a student, you can get the Windows 7 Home edition for $40CND.