Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:31:40+00:00 2026-05-11T09:31:40+00:00

Start with these simple classes… Let’s say I have a simple set of classes

  • 0

Start with these simple classes…

Let’s say I have a simple set of classes like this:

class Bus {     Driver busDriver = new Driver(); }  class Driver {     Shoe[] shoes = { new Shoe(), new Shoe() }; }  class Shoe {     Shoelace lace = new Shoelace(); }  class Shoelace {     bool tied = false; } 

A Bus has a Driver, the Driver has two Shoes, each Shoe has a Shoelace. All very silly.

Add an IDisposable object to Shoelace

Later I decide that some operation on the Shoelace could be multi-threaded, so I add an EventWaitHandle for the threads to communicate with. So Shoelace now looks like this:

class Shoelace {     private AutoResetEvent waitHandle = new AutoResetEvent(false);     bool tied = false;     // ... other stuff .. } 

Implement IDisposable on Shoelace

But now Microsoft’s FxCop will complain: ‘Implement IDisposable on ‘Shoelace’ because it creates members of the following IDisposable types: ‘EventWaitHandle’.’

Okay, I implement IDisposable on Shoelace and my neat little class becomes this horrible mess:

class Shoelace : IDisposable {     private AutoResetEvent waitHandle = new AutoResetEvent(false);     bool tied = false;     private bool disposed = false;      public void Dispose()     {         Dispose(true);         GC.SuppressFinalize(this);     }      ~Shoelace()     {         Dispose(false);     }      protected virtual void Dispose(bool disposing)     {         if (!this.disposed)         {             if (disposing)             {                 if (waitHandle != null)                 {                     waitHandle.Close();                     waitHandle = null;                 }             }             // No unmanaged resources to release otherwise they'd go here.         }         disposed = true;     } } 

Or (as pointed out by commenters) since Shoelace itself has no unmanaged resources, I might use the simpler dispose implementation without needing the Dispose(bool) and Destructor:

class Shoelace : IDisposable {     private AutoResetEvent waitHandle = new AutoResetEvent(false);     bool tied = false;      public void Dispose()     {         if (waitHandle != null)         {             waitHandle.Close();             waitHandle = null;         }         GC.SuppressFinalize(this);     } } 

Watch in horror as IDisposable spreads

Right that’s that fixed. But now FxCop will complain that Shoe creates a Shoelace, so Shoe must be IDisposable too.

And Driver creates Shoe so Driver must be IDisposable. And Bus creates Driver so Bus must be IDisposable and so on.

Suddenly my small change to Shoelace is causing me a lot of work and my boss is wondering why I need to checkout Bus to make a change to Shoelace.

The Question

How do you prevent this spread of IDisposable, but still ensure that your unmanaged objects are properly disposed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:31:41+00:00Added an answer on May 11, 2026 at 9:31 am

    You can’t really ‘prevent’ IDisposable from spreading. Some classes need to be disposed, like AutoResetEvent, and the most efficient way is to do it in the Dispose() method to avoid the overhead of finalizers. But this method must be called somehow, so exactly as in your example the classes that encapsulate or contain IDisposable have to dispose these, so they have to be disposable as well, etc. The only way to avoid it is to:

    • avoid using IDisposable classes where possible, lock or wait for events in single places, keep expensive resources in single place, etc
    • create them only when you need them and dispose them just after (the using pattern)

    In some cases IDisposable can be ignored because it supports an optional case. For example, WaitHandle implements IDisposable to support a named Mutex. If a name is not being used, the Dispose method does nothing. MemoryStream is another example, it uses no system resources and its Dispose implementation also does nothing. Careful thinking about whether an unmanaged resource is being used or not can be instructional. So can examining the available sources for the .net libraries or using a decompiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I think you are looking for the jQuery highlight plugin.… May 11, 2026 at 11:58 am
  • added an answer An easier way to do this would be to set… May 11, 2026 at 11:58 am
  • added an answer Actually I found the answer in 'Areas', which is supposedly… May 11, 2026 at 11:58 am

Related Questions

/* I start with this: */ <Report> <prop1>4</prop1> <prop2>2255</prop2> <prop3>true</prop3> <prop4>false</prop4> <prop5>true</prop5> </Report> /*
Should I start with Django or JavaScript?
I frequently start with a simple console application to try out an idea, then
Let me start with a specific example of what I'm trying to do. I
I'm about to start (with fellow programmers) a programming & algorithms club in my
Apache just fails to start with NO error message when we try to reboot
Let's start with the following snippet: Foreach(Record item in RecordList){ .. item = UpdateRecord(item,
If I start with a Eclipse IDE for Java Developers (85 MB) (Ganymede) installation.
Allow me to start with: I am a n00b on ASP.NET MVC. I love
I want to start with an audio file of a modest filesize, and finish

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.