Implementing IDisposable on a Stream subclass

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Neganov

    Implementing IDisposable on a Stream subclass

    Hi,

    I have a custom subclass of System.IO.Strea m 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.Dis pose() 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

    #2
    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(!AlreadyClos ed) {
    // do my own cleanup
    AlreadyClosed = true;
    }
    }

    "Ivan Neganov" <anonymous@disc ussions.microso ft.com> wrote in message
    news:2b9701c3a8 74$740f5360$310 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > I have a custom subclass of System.IO.Strea m 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.Dis pose() 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]


    Comment

    • Rakesh Namineni[MSFT]

      #3
      Re: Implementing IDisposable on a Stream subclass

      Add the GC.SuppressFina lize to the close method to avoid being called twice
      to clean up resources.

      A Dispose method should call the GC.SuppressFina lize method for the object
      it is disposing. If the object is currently on the finalization queue,
      GC.SuppressFina lize 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(!AlreadyClos ed) {
      // do my own cleanup
      AlreadyClosed = true;
      }
      GC.SuppressFina lize(this);
      }


      --------------------[color=blue]
      >From: "Ted Miller" <ted@nwlink.com >
      >Newsgroups: microsoft.publi c.dotnet.langua ges.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$7 40f5360$3101280 a@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.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-onlin
      e.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn-xit-03!sn-xit-04!sn-
      xit-01!sn-post-01!supernews.co m!corp.supernew s.com!not-for-mail[color=blue]
      >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1984 49
      >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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(!AlreadyClos ed) {
      > // do my own cleanup
      > AlreadyClosed = true;
      > }
      >}
      >
      >"Ivan Neganov" <anonymous@disc ussions.microso ft.com> wrote in message
      >news:2b9701c3a 874$740f5360$31 01280a@phx.gbl. ..[color=green]
      >> Hi,
      >>
      >> I have a custom subclass of System.IO.Strea m 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.Dis pose() 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
      Use these online forms to report copyright and trademark infringement to Microsoft Legal. Infringement notices must comply with the Digital Millennium Copyright Act.


      Comment

      • Ivan Neganov

        #4
        Re: Implementing IDisposable on a Stream subclass

        Thanks a lot!
        [color=blue]
        >-----Original Message-----
        >Add the GC.SuppressFina lize to the close method to avoid[/color]
        being called twice[color=blue]
        >to clean up resources.
        >
        >A Dispose method should call the GC.SuppressFina lize[/color]
        method for the object[color=blue]
        >it is disposing. If the object is currently on the[/color]
        finalization queue,[color=blue]
        >GC.SuppressFin alize 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(!AlreadyClos ed) {
        > // do my own cleanup
        > AlreadyClosed = true;
        > }
        > GC.SuppressFina lize(this);
        >}
        >
        >
        >--------------------[color=green]
        >>From: "Ted Miller" <ted@nwlink.com >
        >>Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        >>Subject: Re: Implementing IDisposable on a Stream[/color][/color]
        subclass[color=blue][color=green]
        >>Date: Tue, 11 Nov 2003 10:17:44 -0800
        >>Organizatio n: Posted via Supernews,[/color][/color]
        http://www.supernews.com[color=blue][color=green]
        >>Message-ID: <vr29tui03ivn5c @corp.supernews .com>
        >>References: <2b9701c3a874$7 40f5360$3101280 a@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.p hx.gbl!TK2MSFTN GP08.phx.gbl![/color]
        newsfeed00.sul. t-online.de!t-onlin[color=blue]
        >e.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn-[/color]
        xit-03!sn-xit-04!sn-[color=blue]
        >xit-01!sn-post-01!supernews.co m!corp.supernew s.com!not-[/color]
        for-mail[color=blue][color=green]
        >>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        microsoft.publi c.dotnet.langua ges.csharp:1984 49[color=blue][color=green]
        >>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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(!AlreadyClos ed) {
        >> // do my own cleanup
        >> AlreadyClosed = true;
        >> }
        >>}
        >>
        >>"Ivan Neganov" <anonymous@disc ussions.microso ft.com>[/color][/color]
        wrote in message[color=blue][color=green]
        >>news:2b9701c3 a874$740f5360$3 101280a@phx.gbl ...[color=darkred]
        >>> Hi,
        >>>
        >>> I have a custom subclass of System.IO.Strea m 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.Dis pose() 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]

        Comment

        Working...