Sub Dispose or Implementing the Dispose Interface?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott M.

    Sub Dispose or Implementing the Dispose Interface?

    In a typical class, do I need to indicate that it implements the IDisposable
    interface and then create a Dispose method that implements the Dispose
    required by the IDisposable interface or can I just make a Sub Dispose() and
    the CLR will know that this is the Dispose method to call when the object
    falls out of scope?

    Thanks.


  • Imran Koradia

    #2
    Re: Sub Dispose or Implementing the Dispose Interface?

    The runtime (CLR) never calls the dispose method. It is upto the client
    using the object to call dispose. There are a couple of exceptions. Read
    this for more information:


    So whether you just add a Dispose method or implement IDisposable interface,
    its upto the application using your class to call the dispose method.
    Ofcourse, it makes more sense to implement IDisposable since that is indeed
    why the interface is there - for one to write dispose code. If you simply
    write a Sub Dispose, it could be misleading to someone using your class
    since one would expect an implementation of IDisposable if one were to
    follow the standards.


    hope that helps..
    Imran.

    "Scott M." <NoSpam@NoSpam. com> wrote in message
    news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=blue]
    > In a typical class, do I need to indicate that it implements the
    > IDisposable interface and then create a Dispose method that implements the
    > Dispose required by the IDisposable interface or can I just make a Sub
    > Dispose() and the CLR will know that this is the Dispose method to call
    > when the object falls out of scope?
    >
    > Thanks.
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Sub Dispose or Implementing the Dispose Interface?

      Scott,
      The framework itself never calls Dispose.

      C# will call IDisposable.Dis pose if you use the object in a Using statement.

      VB.NET 2002 & 2003 & C#'s For Each statements will call IDisposable.Dis pose
      if its part of a class that implements IEnumerator.

      Otherwise VB.NET (2002 & 2003) will not call IDisposable.Dis pose.

      VB.NET 2005 (aka Whidbey, due out later in 2005) will add support to VB.NET
      for the Using statement, the Using statement will call IDisposable.Dis pose
      on the End Using line.

      Information on VB.NET 2005:


      Information on VB.NET's Using statement:


      An object falling out of scope never calls Dispose nor IDisposable.Dis pose.


      If you are considering implementing IDisposable, you should also consider
      implementing Finalize also.

      The "Implementi ng Finalize and Dispose to Clean Up Unmanaged Resources" on
      MSDN:



      Covers when you should implement Finalize, when you should implement both,
      and when you should implement just IDisposable.

      Hope this helps
      Jay





      "Scott M." <NoSpam@NoSpam. com> wrote in message
      news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=blue]
      > In a typical class, do I need to indicate that it implements the
      > IDisposable interface and then create a Dispose method that implements the
      > Dispose required by the IDisposable interface or can I just make a Sub
      > Dispose() and the CLR will know that this is the Dispose method to call
      > when the object falls out of scope?
      >
      > Thanks.
      >[/color]


      Comment

      • Scott M.

        #4
        Re: Sub Dispose or Implementing the Dispose Interface?

        Ok Imran, then let me ask you this...

        When the GC does eventually collect the garbage, what determines which
        objects are collected and which are not? Is is simply that any object that
        is no longer referenced are the ones collected?


        "Imran Koradia" <nojunk@microso ft.com> wrote in message
        news:%236sL60xt EHA.2596@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > The runtime (CLR) never calls the dispose method. It is upto the client
        > using the object to call dispose. There are a couple of exceptions. Read
        > this for more information:
        > http://blogs.msdn.com/clyon/archive/...21/232445.aspx
        >
        > So whether you just add a Dispose method or implement IDisposable
        > interface, its upto the application using your class to call the dispose
        > method. Ofcourse, it makes more sense to implement IDisposable since that
        > is indeed why the interface is there - for one to write dispose code. If
        > you simply write a Sub Dispose, it could be misleading to someone using
        > your class since one would expect an implementation of IDisposable if one
        > were to follow the standards.
        >
        >
        > hope that helps..
        > Imran.
        >
        > "Scott M." <NoSpam@NoSpam. com> wrote in message
        > news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=green]
        >> In a typical class, do I need to indicate that it implements the
        >> IDisposable interface and then create a Dispose method that implements
        >> the Dispose required by the IDisposable interface or can I just make a
        >> Sub Dispose() and the CLR will know that this is the Dispose method to
        >> call when the object falls out of scope?
        >>
        >> Thanks.
        >>[/color]
        >
        >[/color]


        Comment

        • Cor Ligthert

          #5
          Re: Sub Dispose or Implementing the Dispose Interface?

          Scott,

          When you use the componenttempla te to create the class, it gives you a
          complete class where IDisposable setting is completly done for you.

          In any class that implements IDisposable is the disposing from the resourses
          used in that class done for you.

          Cor


          "Scott M." <NoSpam@NoSpam. com> schreef in bericht
          news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=blue]
          > In a typical class, do I need to indicate that it implements the
          > IDisposable interface and then create a Dispose method that implements the
          > Dispose required by the IDisposable interface or can I just make a Sub
          > Dispose() and the CLR will know that this is the Dispose method to call
          > when the object falls out of scope?
          >
          > Thanks.
          >[/color]


          Comment

          • Larry Serflaten

            #6
            Re: Sub Dispose or Implementing the Dispose Interface?


            "Scott M." <NoSpam@NoSpam. com> wrote[color=blue]
            > Ok Imran, then let me ask you this...
            >
            > When the GC does eventually collect the garbage, what determines which
            > objects are collected and which are not? Is is simply that any object that
            > is no longer referenced are the ones collected?[/color]


            Its not quite that simple. The GC is good at cleaning up short lived objects,
            but those that hang around for a while get shoved on 'the back burner' (meaning
            they don't get tested as often).

            The GC is also pretty good at managing those long lived objects, but its the
            middle ground that needs to be addressed. That area is dependant on your
            own use of memory, so just how often the GC runs, and decides to move
            things to the 'back burner' is partially dependant on what you're doing....

            FWIW:
            The proper term is 'generations'. Generation 0 items come and go as needed....

            So, its those Gen 0 items that are swept away when nothing holds a reference
            to them. If they survive that first look, then get shoved into the Gen 1 area, and
            then Gen 2, etc where they don't go looking for memory as often as the Gen 0 area.

            For a bit more on the GC, you might want to watch the .Net show where one
            of the CLR's architect was interviewed:
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


            LFS

            Comment

            • Imran Koradia

              #7
              Re: Sub Dispose or Implementing the Dispose Interface?

              Scott,

              As Larry mentioned, collection is a bit more complicated. I think you are
              confusing a finalize method with the dispose method (I could be wrong
              though). If an object has a finalizer, it is not collected (when a
              collection is performed by the GC and it finds that the object is not
              referenced anymore) but instead is pushed onto a finalization queue so that
              the runtime can execute the finalize method of the object. And then there is
              the concept of various generations (0, 1 and 2 in the current framework
              version). I would suggest you read these 2 excellent articles by Jeffrey
              Richter on Garbage collection which also talks about finalization and
              resurrection:
              Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

              Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


              Also, here is a blog post on dispose dos and don'ts by Chris Lyon which you
              might be interested in since you are deciding to implement dispose in your
              class:



              hope that helps..
              Imran.

              "Scott M." <NoSpam@NoSpam. com> wrote in message
              news:Ogvt7aytEH A.1272@TK2MSFTN GP12.phx.gbl...[color=blue]
              > Ok Imran, then let me ask you this...
              >
              > When the GC does eventually collect the garbage, what determines which
              > objects are collected and which are not? Is is simply that any object
              > that is no longer referenced are the ones collected?
              >
              >
              > "Imran Koradia" <nojunk@microso ft.com> wrote in message
              > news:%236sL60xt EHA.2596@TK2MSF TNGP10.phx.gbl. ..[color=green]
              >> The runtime (CLR) never calls the dispose method. It is upto the client
              >> using the object to call dispose. There are a couple of exceptions. Read
              >> this for more information:
              >> http://blogs.msdn.com/clyon/archive/...21/232445.aspx
              >>
              >> So whether you just add a Dispose method or implement IDisposable
              >> interface, its upto the application using your class to call the dispose
              >> method. Ofcourse, it makes more sense to implement IDisposable since that
              >> is indeed why the interface is there - for one to write dispose code. If
              >> you simply write a Sub Dispose, it could be misleading to someone using
              >> your class since one would expect an implementation of IDisposable if one
              >> were to follow the standards.
              >>
              >>
              >> hope that helps..
              >> Imran.
              >>
              >> "Scott M." <NoSpam@NoSpam. com> wrote in message
              >> news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=darkred]
              >>> In a typical class, do I need to indicate that it implements the
              >>> IDisposable interface and then create a Dispose method that implements
              >>> the Dispose required by the IDisposable interface or can I just make a
              >>> Sub Dispose() and the CLR will know that this is the Dispose method to
              >>> call when the object falls out of scope?
              >>>
              >>> Thanks.
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Scott M.

                #8
                Re: Sub Dispose or Implementing the Dispose Interface?

                Yes, I know that Cor. My question was about classes that are built from
                scratch.


                "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                news:eCat8T0tEH A.3932@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Scott,
                >
                > When you use the componenttempla te to create the class, it gives you a
                > complete class where IDisposable setting is completly done for you.
                >
                > In any class that implements IDisposable is the disposing from the
                > resourses used in that class done for you.
                >
                > Cor
                >
                >
                > "Scott M." <NoSpam@NoSpam. com> schreef in bericht
                > news:uILNuOxtEH A.2196@TK2MSFTN GP14.phx.gbl...[color=green]
                >> In a typical class, do I need to indicate that it implements the
                >> IDisposable interface and then create a Dispose method that implements
                >> the Dispose required by the IDisposable interface or can I just make a
                >> Sub Dispose() and the CLR will know that this is the Dispose method to
                >> call when the object falls out of scope?
                >>
                >> Thanks.
                >>[/color]
                >
                >[/color]


                Comment

                Working...