Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IDisposable on a subclass when the parent also implements IDisposable

I have a parent and child class that both need to implement IDisposable. Where should virtual (and base.Dispose()?) calls come into play? When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), but having everything else.

What I had been doing (trivialized quite a bit):

internal class FooBase : IDisposable {     Socket baseSocket;      private void SendNormalShutdown() { }      public void Dispose()     {         Dispose(true);         GC.SuppressFinalize(this);     }      private bool _disposed = false;     protected virtual void Dispose(bool disposing)     {         if (!_disposed)         {             if (disposing)             {                 SendNormalShutdown();             }             baseSocket.Close();         }     }      ~FooBase()     {         Dispose(false);     } }  internal class Foo : FooBase, IDisposable {     Socket extraSocket;      private bool _disposed = false;     protected override void Dispose(bool disposing)     {         if (!_disposed)         {             extraSocket.Close();         }         base.Dispose(disposing);     }      ~Foo()     {         Dispose(false);     }  } 
like image 448
Tanzelax Avatar asked Mar 22 '10 22:03

Tanzelax


People also ask

When should you implement IDisposable?

in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed. If not, this responsibility is left to the garbage collector to free the memory when the object containing the unmanaged resources is finalized.

Does not implement IDisposable Dispose?

If an object doesn't implement IDisposable , then you don't have to dispose of it. An object will only expose Dispose if it needs to be disposed of.

What is IDisposable interface in C implement the Dispose method?

The Dispose method is automatically called when a using statement is used. All the objects that can implement the IDisposable interface can implement the using statement. You can use the ildasm.exe tool to check how the Dispose method is called internally when you use a using statement.

What is the purpose of the IDisposable interface?

IDisposable interface is to release unmanaged resources in C#.NET. IDisposable interface is to release unmanaged resources. This framework would detect that an object is no longer needed as soon as it occurs and automatically free up the memory.


1 Answers

When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), but having everything else.

This is something you shouldn't be concerned with.

When you subclass an IDisposable class, all of the "Dispose pattern" plumbing is already being handled for you by the base class. You really should do nothing but override the protected Dispose(bool) method, and track whether you've been disposed already (to properly raise ObjectDisposedException.)

For details, see my blog post on Subclassing from an IDisposable class.


Also, often, it's a good idea to consider encapsulating the IDisposable class instead of subclassing it. There are times when subclassing an IDisposable class is appropriate, but they are somewhat rare. Encapsulation is often a better alternative.

like image 153
Reed Copsey Avatar answered Oct 01 '22 06:10

Reed Copsey