Back in my day… we called that a memory leak

 .NET is cool, but it still gives me fits sometimes.  I was writing a test and in my fixture I was checking that the events I set up on my class where getting fired correctly.  All things are going well on each individual test, but when I ran them all together, bang, red light, wooop wooop, Scotty… what is going on down there?   “Don’t know cap’n”.  Now wait a minute, I hooked all the events right and in each test I created a new object in isolation (good thing).  Things seem alright, until my old school ways bit me in the a33.  I keep forgetting the “garbage” lets my objects hang around till it feels ok about collecting it. The solution was to remove the delegate for the event before I left the scope of the method.  So potentially back in my day I would call that a memory leak, but in reality, I am just plain ignorant.

Comments (3)

  1. Steve says:

    Funny you mention this. I’ve caught myself not removing delegates before, and every time I do it always causes me to wonder how many people don’t remove them from objects going out of scope. Somewhere deep inside many systems there are probably a slew of objects handling events, even though they are no longer referenced anymore.

    A simple koan from teacher to student:

    Teacher: "What is the sound of one event firing?"

    Student: "In some systems, the sound of chaos, teach’."

    ~Steve Seymour