Connecting Tech Pros Worldwide Forums | Help | Site Map

Implementing IDisposable on a Stream subclass

Ivan Neganov
Guest
 
Posts: n/a
#1: Nov 15 '05
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.

Ted Miller
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Implementing IDisposable on a Stream subclass


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;
}
}

"Ivan Neganov" <anonymous@discussions.microsoft.com> wrote in message
news:2b9701c3a874$740f5360$3101280a@phx.gbl...[color=blue]
> 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.[/color]


Rakesh Namineni[MSFT]
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Implementing IDisposable on a Stream subclass


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);
}


--------------------[color=blue]
>From: "Ted Miller" <ted@nwlink.com>
>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: <vr29tui03ivn5c@corp.supernews.com>
>References: <2b9701c3a874$740f5360$3101280a@phx.gbl>
>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: abuse@supernews.com
>Lines: 45
>Path:[/color]
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.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[color=blue]
>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;
> }
>}
>
>"Ivan Neganov" <anonymous@discussions.microsoft.com> wrote in message
>news:2b9701c3a874$740f5360$3101280a@phx.gbl...[color=green]
>> 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.[/color]
>
>
>[/color]


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

Ivan Neganov
Guest
 
Posts: n/a
#4: Nov 15 '05

re: Implementing IDisposable on a Stream subclass


Thanks a lot!
[color=blue]
>-----Original Message-----
>Add the GC.SuppressFinalize to the close method to avoid[/color]
being called twice[color=blue]
>to clean up resources.
>
>A Dispose method should call the GC.SuppressFinalize[/color]
method for the object[color=blue]
>it is disposing. If the object is currently on the[/color]
finalization queue,[color=blue]
>GC.SuppressFinalize prevents its Finalize method from[/color]
being called.[color=blue]
>Remember that executing a Finalize method is costly to[/color]
performance. If your[color=blue]
>Dispose method has already done the work to clean up the[/color]
object, then it is[color=blue]
>not necessary for the garbage collector to call the[/color]
object's Finalize[color=blue]
>method.
>
>
>public override Close()
>{
> base.Close();
> if(!AlreadyClosed) {
> // do my own cleanup
> AlreadyClosed = true;
> }
> GC.SuppressFinalize(this);
>}
>
>
>--------------------[color=green]
>>From: "Ted Miller" <ted@nwlink.com>
>>Newsgroups: microsoft.public.dotnet.languages.csharp
>>Subject: Re: Implementing IDisposable on a Stream[/color][/color]
subclass[color=blue][color=green]
>>Date: Tue, 11 Nov 2003 10:17:44 -0800
>>Organization: Posted via Supernews,[/color][/color]
http://www.supernews.com[color=blue][color=green]
>>Message-ID: <vr29tui03ivn5c@corp.supernews.com>
>>References: <2b9701c3a874$740f5360$3101280a@phx.gbl>
>>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: abuse@supernews.com
>>Lines: 45
>>Path:[/color]
>cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl![/color]
newsfeed00.sul.t-online.de!t-onlin[color=blue]
>e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-[/color]
xit-03!sn-xit-04!sn-[color=blue]
>xit-01!sn-post-01!supernews.com!corp.supernews.com!not-[/color]
for-mail[color=blue][color=green]
>>Xref: cpmsftngxa06.phx.gbl[/color][/color]
microsoft.public.dotnet.languages.csharp:198449[color=blue][color=green]
>>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
>>
>>The Close method is virtual. The base class will call[/color][/color]
Close when it's[color=blue][color=green]
>>disposed or finalized.
>>
>>The bottom line is that I think all you need to do is[/color][/color]
implement a Close[color=blue][color=green]
>>method like so:
>>
>>public override Close()
>>{
>> base.Close();
>> if(!AlreadyClosed) {
>> // do my own cleanup
>> AlreadyClosed = true;
>> }
>>}
>>
>>"Ivan Neganov" <anonymous@discussions.microsoft.com>[/color][/color]
wrote in message[color=blue][color=green]
>>news:2b9701c3a874$740f5360$3101280a@phx.gbl...[color=darkred]
>>> Hi,
>>>
>>> I have a custom subclass of System.IO.Stream type. I
>>> wonder how to correctly implement the IDisposable[/color][/color][/color]
pattern[color=blue][color=green][color=darkred]
>>> in this situation.
>>>
>>> The parent Stream type apparently uses explicit[/color][/color][/color]
interface[color=blue][color=green][color=darkred]
>>> 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[/color][/color][/color]
like[color=blue][color=green][color=darkred]
>>> Jeff Ricther recommends in his book.
>>>
>>> On the other hand, the Framework types like FileStream
>>> seem to somehow override their parent Stream's Dispose[/color][/color][/color]
()[color=blue][color=green][color=darkred]
>>> method...
>>>
>>> Any ideas?
>>>
>>> Thanks,
>>>
>>> Ivan.[/color]
>>
>>
>>[/color]
>
>
>Rakesh, EFT.
>
>This posting is provided "AS IS" with no warranties, and[/color]
confers no rights.[color=blue]
>Use of included script samples are subject to the terms[/color]
specified at[color=blue]
>http://www.microsoft.com/info/cpyright.htm
>
>.
>[/color]
Closed Thread


Similar C# / C Sharp bytes