Mar 192010
 

Data hiding and abstraction;  two of the great benefits of object oriented programming.  Sometimes, however, they need to be left behind because you actually need to know what’s going on.

Take for example an image object I wrote that somebody was trying to access pixel by pixel.  It’s a c++ class with a COM interface so that it can be easily used in lots of other places.    There probably wouldn’t have been any problems if the images were small, say 640×480 pixels.  But we’re talking about 4000×4000 pixels, and 16 bits per pixel.

The offending code was something like:

myVal = myImage.PixelArray(x, y);

This looks innocent enough.  But why does the code apparently hang when you loop through the entire image?   Well, it’s because PixelArray returns a SAFEARRAY that the image object creates to give to the caller so that they can do whatever they want with their own copy image data, in a format that they’re familiar with.  That’s a 32MB copy.  Then for some reason the programmer takes one value out of that array and then throws the rest away.  It’s like calling up a dump truck full of gravel and then just taking one shovel full out and then sending the truck away…and doing that 32 million times.

Another exciting one involving large objects is garbage collection.  It’s so nice – you just throw things over your shoulder and the run time environment looks after cleaning up.  Works great until you start working with objects that the run time environment doesn’t understand (like a COM image object in .NET) so it can’t tell whether there’s a lot of stuff in the garbage bin.  We had an application that would run out of memory in under a minute.  We spent quite a bit of time searching for the leak and then discovered that we had to prod the automatic garbage collection frequently.   I guess this shouldn’t be so surprising: if you renovate your house you should probably order up a large bin rather than relying on the weekly collection.

Well, I’m not the first to talk about this sort of stuff;  Joel Spolsky’s article on the Law of Leaky Abstractions went into some more detail.

 Posted by at 11:36 pm