The Perils of Football Pads

I’ve always wondered why rugby players seem to get injured less than Football players. It doesn’t make sense to me. Football players have pads, Rugby players do not. One is protected, the other is not. One gets injured, the other does not. QED. 

So why in the name of Chuck Norris do Football players have more frequent and more extreme injuries than Rugby players?

The answer is in the pads:

[UMKC physics professor Robbert] Riggs believes there would be fewer serious injuries if players didn’t wear pads or helmets.

“As a retired Marine officer,” Riggs said, “I know that when you’re wearing body armor, sometimes you feel invincible and I think there’s some of that. They put on all those pads and they feel invincible.” (emphasis mine)

Sound familiar? (warning, PDF)

The developer does not need to explicitly free memory assigned to an object, which goes a long way toward reducing the amount of time spent debugging memory leaks. (10) There can be no memory leaks in 100% managed code.

Not only is this statement downright false, it’s also dangerous.  

Google currently has 103,000 hits for “Memory Leak C#”, and they all contain a similar tale of woe, programmers telling other programmers how not to make the same mistake they did. Each example boils down to one of three things:

1. Not de-referencing an event handler:

MyType object2 = new MyType();

// …
object1.SomeEvent += object2.myEventHandler;
// …

// Should call this
// object1.SomeEvent -= object2.myEventHandler;

object2.Dispose();

My most recent encounter with a memory leak was much like this: There was a class that dealt with dynamically wiring up events for other objects (as if they couldn’t do it themselves?) and although these references went out of scope, the event handlers were still hanging around, subscribing to references that were now gone. Anything that was being used as a reference couldn’t be garbage-collected, resulting in hundreds of megabytes of memory being retained over the course of an application’s life.

2. Creating global references to objects:

I don’t care what Greenpeace tells you: Think locally, not globally. Objects that don’t have external references are much easier to keep track of than those that do. 

3. Disposing of Forms

If you’re a Winforms or Webforms developer, this one is for you. ASP.NET MVC guys get a pass because they wised up. If you’re using something that isn’t yours, Dispose of it. Period. Database Handles, Filehandles, Controls, Forms, Whatever. Dispose of it. 

Bad decisions have a half-life that rivals uranium. Don’t make bad decisions: Know your code.

Leave a Reply