Implementing IDisposable on a Stream subclass

  • Thread starter Thread starter Ivan Neganov
  • Start date Start date
I

Ivan Neganov

Hi,

I have a custom subclass of System.IO.Stream type. I
wonder how to correctly implement the IDisposable pattern
in this situation.

The parent Stream type apparently uses explicit interface
implementation, and I could not find a way for my child
type to override parent's IDisposable.Dispose() method.
The intention was to clean up child's resources first,
then call parent's Dispose() method.

What first comes to mind is to inherit the IDisposable
interface directly in my child type, despite the parent
already does so, and implement my own Dispose pattern like
Jeff Ricther recommends in his book.

On the other hand, the Framework types like FileStream
seem to somehow override their parent Stream's Dispose()
method...

Any ideas?

Thanks,

Ivan.
 
The Close method is virtual. The base class will call Close when it's
disposed or finalized.

The bottom line is that I think all you need to do is implement a Close
method like so:

public override Close()
{
base.Close();
if(!AlreadyClosed) {
// do my own cleanup
AlreadyClosed = true;
}
}
 
Add the GC.SuppressFinalize to the close method to avoid being called twice
to clean up resources.

A Dispose method should call the GC.SuppressFinalize method for the object
it is disposing. If the object is currently on the finalization queue,
GC.SuppressFinalize prevents its Finalize method from being called.
Remember that executing a Finalize method is costly to performance. If your
Dispose method has already done the work to clean up the object, then it is
not necessary for the garbage collector to call the object's Finalize
method.


public override Close()
{
base.Close();
if(!AlreadyClosed) {
// do my own cleanup
AlreadyClosed = true;
}
GC.SuppressFinalize(this);
}


--------------------
From: "Ted Miller" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Re: Implementing IDisposable on a Stream subclass
Date: Tue, 11 Nov 2003 10:17:44 -0800
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <[email protected]>
References: <[email protected]>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Complaints-To: (e-mail address removed)
Lines: 45
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-04!sn-
xit-01!sn-post-01!supernews.com!corp.supernews.com!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198449
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

The Close method is virtual. The base class will call Close when it's
disposed or finalized.

The bottom line is that I think all you need to do is implement a Close
method like so:

public override Close()
{
base.Close();
if(!AlreadyClosed) {
// do my own cleanup
AlreadyClosed = true;
}
}


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Thanks a lot!
-----Original Message-----
Add the GC.SuppressFinalize to the close method to avoid being called twice
to clean up resources.

A Dispose method should call the GC.SuppressFinalize method for the object
it is disposing. If the object is currently on the finalization queue,
GC.SuppressFinalize prevents its Finalize method from being called.
Remember that executing a Finalize method is costly to performance. If your
Dispose method has already done the work to clean up the object, then it is
not necessary for the garbage collector to call the object's Finalize
method.


public override Close()
{
base.Close();
if(!AlreadyClosed) {
// do my own cleanup
AlreadyClosed = true;
}
GC.SuppressFinalize(this);
}


--------------------
newsfeed00.sul.t-online.de!t-onlin


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

.
 
Back
Top