The World According to Nick
My take on Software, Technology, Politics, and anything else I feel like talking about.
Thursday, May 27, 2004

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.

Comments:

Post a Comment

Related Links:

About Me



Name: Nick
Home: Wauwatosa, WI, United States

I'm a Software Consultant in the Milwaukee area. Among various geeky pursuits, I'm also an amateur triathlete, and enjoy rock climbing. I also like to think I'm a political pundit.


 View My Profile

Archives
 Home Page

Subscribe to this Feed

Search Archives
Previous Posts
Helping Young Drivers
CodeProject for Anyone
Word of the Day
When Is a Convention Not Really a Convention?
Blog vs. Surf
Care to Speak About This Ted?
I Didn't Say That... My Speechwriters Did
Programming in the Green
Destroying Elegant... no... Beautiful Code
Did You Know?

Personal Links
Carnival of the Badger
The Coding Monkey
del.icio.us Links
Flickr Photos
Blog Critics Reviews





Blogroll Me!

music
books
video
culture
politics
sports
gaming

www.flickr.com
This is a Flickr badge showing public photos from Nick_Schweitzer. Make your own badge here.

Credits

Blogcritics: news and reviews







This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com

RSS-to-JavaScript.com

Listed on BlogShares

Design By maystar