Sunday, 30 October, 2005

Memory leaks and reference counting

It's not that I have nothing to say, but rather I don't have time to say it.  I've been hard at work these last few weeks on the project, trying to make sure that everything we need for the end of month deliverable is ready.  And oh, boy, did I run into a fun one.

Back on the 19th I mentioned that I was having trouble with memory leaks.  That turned out to be just the start of my problem.  Memory management is hard--really hard--and even really good programmers seem to have trouble with it even in relatively simple programs.  In complex programs, where several subsystems need references to a single object, it gets even more complex.  A very common problem is trying to dereference an object that's already been destroyed.  Imagine, Subsystem A creates an object.  Subsystem B obtains a reference to the object.  After a while, Subsystem A destroys the object.  Bad things happen when Subsystem B then tries to access the destroyed object.  This is what I call "pulling the rug out from under me."

A common solution to this problem is reference counting.  When Subsystem A creates the object, the object's reference count is set to 1.  When Subsystem B obtains a reference, the reference count is increased to 2.  Now, when Subsystem A "destroys" the object, the reference count is set back to 1 but the object isn't actually destroyed.  It will only be destroyed when the reference count goes to zero (i.e., when Subsystem B releases the reference).

Reference counting works if everybody follows the rules.  But what happens all too often is that somebody doesn't follow the rules (surprise, surprise), and all of a sudden you're back to memory leak land except that it's more difficult to track down who actually is the culprit.  Who didn't release the object?  Oh, it do get ugly in a hurry.

The really frightening part is that people sometimes implement reference counting in order to eliminate memory leaks.  There's a good idea!  Solve a complicated problem (memory management) by putting an even more complicated problem (reference counting) on top of it.  Whoever thought that was a good idea should have his fingers glued together so that he can't do any more programming.

Tracking down memory leaks and mis-matched object reference counts is why you haven't been hearing much from me lately.