The World According to Nick
Politics, News, Photography, and Triathlons... What don't I talk about?
Thursday, May 27, 2004
<< Helping Young Drivers Close all Windows then Restart >>
Don't Feed the Immutable Structs
Found this little story of a programming gotcha at Peter Golde's Weblog via Cyrus's Weblog (he's a VS.NET Core guy at Microsoft). It talks about the dangers of having immutable objects within a struct (which is mutable)... Here is the code snippet that caused him issues:

struct FooPart
{
   // other data (which was immutable)
   Pen pen;

   public void ReleasePens()
   {
      if ( pen != null )
      {
         pen.Dispose();
         pen = null;
      }
   }
}

class Foo
{
   FooPart[] parts;

   public void ReleasePens()
   {
      foreach ( FooPart part in parts )
      {
         part.ReleasePens();
      }
   }
}

So what's wrong with this code? I actually did figure this out as soon as I saw the code... but it's a good reminder anyway. Go to Peter Golde's Weblog for the answer and explanation.
# Posted at 8:35 AM by Nick  |  Comment Feed Link No Comments  |  No Trackbacks

 Add to del.icio.us |  Digg this Post | Filed Under: Old Blog

Comments are closed.


© Copyright 2012 Nick Schweitzer
Powered By newtelligence dasBlog 1.9.7067.0
Theme Based on Design By maystar