Disposing objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Spence1

    Disposing objects

    I'm rather confused as to whether something should be disposed of, or not.
    What is the general rule? How can you be sure of doing the right thing? I've
    heard about disposing unmanaged resources but what is an unmanaged resource?

    Does everything need disposing?

    I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
    sure of what needs disposing?

    Do these get automatically get disposed of at the end of the procedure that
    declares them?

    Is there any way to check my program to see if I have something that needs
    disposing and is still using resources?

    -Jerry


  • Michel Posseth  [MCP]

    #2
    Re: Disposing objects

    Every object that implements an idisposable interface should be disposed of
    i recomend you to write these objects in a using en using bloock

    like this

    Using Foo as new Foo.Lib

    '''do your stuff

    End using 'the end using will take care of disposing so no need to call it

    also note that a declared variabel in a using block gets a block scope

    Using Foo as new Foo.Lib
    Dim Bla As string 'only accessible in the using block
    '''do your stuff

    End using



    A unmanagaed resource is a resource that is not native to .Net but started
    through interop , PInvoke etc etc
    for instance API calls or COM objects

    Object pointers that go out of scope will eventually be cleaned by the GC
    however how and when this happens is a mystery that only the GC knows :-)

    hth

    Michel Posseth [MCP]
    ASP.NET hosting, SQL hosting, AJAX Hosting, Silverlight hosting, LINQ Hosting, Microsoft Windows 2012 hosting, iis8 hosting, Windows 2012 R2 hosting, iis8.5 hosting.






    "Jerry Spence1" <123@dfgh.comsc hreef in bericht
    news:p9adnXVn-LFgiXrVnZ2dnUVZ 8vqdnZ2d@posted .plusnet...
    I'm rather confused as to whether something should be disposed of, or not.
    What is the general rule? How can you be sure of doing the right thing?
    I've heard about disposing unmanaged resources but what is an unmanaged
    resource?
    >
    Does everything need disposing?
    >
    I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
    sure of what needs disposing?
    >
    Do these get automatically get disposed of at the end of the procedure
    that declares them?
    >
    Is there any way to check my program to see if I have something that needs
    disposing and is still using resources?
    >
    -Jerry
    >

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Disposing objects

      Michel,

      Can you show me your latest program where you use the dispose method on
      Label?

      Cor

      "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
      news:uDhDJMfJJH A.468@TK2MSFTNG P06.phx.gbl...
      Every object that implements an idisposable interface should be disposed
      of
      i recomend you to write these objects in a using en using bloock
      >
      like this
      >
      Using Foo as new Foo.Lib
      >
      '''do your stuff
      >
      End using 'the end using will take care of disposing so no need to call
      it
      >
      also note that a declared variabel in a using block gets a block scope
      >
      Using Foo as new Foo.Lib
      Dim Bla As string 'only accessible in the using block
      '''do your stuff
      >
      End using
      >
      >
      >
      A unmanagaed resource is a resource that is not native to .Net but started
      through interop , PInvoke etc etc
      for instance API calls or COM objects
      >
      Object pointers that go out of scope will eventually be cleaned by the GC
      however how and when this happens is a mystery that only the GC knows :-)
      >
      hth
      >
      Michel Posseth [MCP]
      ASP.NET hosting, SQL hosting, AJAX Hosting, Silverlight hosting, LINQ Hosting, Microsoft Windows 2012 hosting, iis8 hosting, Windows 2012 R2 hosting, iis8.5 hosting.

      >
      >
      >
      >
      >
      "Jerry Spence1" <123@dfgh.comsc hreef in bericht
      news:p9adnXVn-LFgiXrVnZ2dnUVZ 8vqdnZ2d@posted .plusnet...
      >I'm rather confused as to whether something should be disposed of, or
      >not. What is the general rule? How can you be sure of doing the right
      >thing? I've heard about disposing unmanaged resources but what is an
      >unmanaged resource?
      >>
      >Does everything need disposing?
      >>
      >I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
      >sure of what needs disposing?
      >>
      >Do these get automatically get disposed of at the end of the procedure
      >that declares them?
      >>
      >Is there any way to check my program to see if I have something that
      >needs disposing and is still using resources?
      >>
      >-Jerry
      >>
      >
      >

      Comment

      • Michel Posseth  [MCP]

        #4
        Re: Disposing objects

        Aha ,, well you wanted more detailed info Cor ;-) ?

        As we both know that calling Dispose in that case is not necesary as
        Dispose is automatically called on all controls and components placed on
        windows forms or user controls.

        However in general the rule is

        dispose everything that implements IDisposable


        regards
        Michel





        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
        news:7428BB1B-7722-4BCD-A3BE-2CBF453FE12B@mi crosoft.com...
        Michel,
        >
        Can you show me your latest program where you use the dispose method on
        Label?
        >
        Cor
        >
        "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
        news:uDhDJMfJJH A.468@TK2MSFTNG P06.phx.gbl...
        >Every object that implements an idisposable interface should be disposed
        >of
        >i recomend you to write these objects in a using en using bloock
        >>
        >like this
        >>
        >Using Foo as new Foo.Lib
        >>
        >'''do your stuff
        >>
        >End using 'the end using will take care of disposing so no need to call
        >it
        >>
        >also note that a declared variabel in a using block gets a block scope
        >>
        >Using Foo as new Foo.Lib
        >Dim Bla As string 'only accessible in the using block
        >'''do your stuff
        >>
        >End using
        >>
        >>
        >>
        >A unmanagaed resource is a resource that is not native to .Net but
        >started through interop , PInvoke etc etc
        >for instance API calls or COM objects
        >>
        >Object pointers that go out of scope will eventually be cleaned by the GC
        >however how and when this happens is a mystery that only the GC knows
        >:-)
        >>
        >hth
        >>
        >Michel Posseth [MCP]
        >http://www.vbdotnetcoder.com
        >>
        >>
        >>
        >>
        >>
        >"Jerry Spence1" <123@dfgh.comsc hreef in bericht
        >news:p9adnXV n-LFgiXrVnZ2dnUVZ 8vqdnZ2d@posted .plusnet...
        >>I'm rather confused as to whether something should be disposed of, or
        >>not. What is the general rule? How can you be sure of doing the right
        >>thing? I've heard about disposing unmanaged resources but what is an
        >>unmanaged resource?
        >>>
        >>Does everything need disposing?
        >>>
        >>I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
        >>sure of what needs disposing?
        >>>
        >>Do these get automatically get disposed of at the end of the procedure
        >>that declares them?
        >>>
        >>Is there any way to check my program to see if I have something that
        >>needs disposing and is still using resources?
        >>>
        >>-Jerry
        >>>
        >>
        >>
        >

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Disposing objects

          Michel,
          However in general the rule is
          dispose everything that implements IDisposable
          >
          Who has given this general rule?
          Or is it "yours" general rule, I don't that it is right to give things on
          this board without telling that it is your general rule.

          That the using has the dispose implemented is obvious, it is easier to
          implement that, then spend time to find out if it should be there.

          Cor

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: Disposing objects

            Jerry Spence1 wrote:
            I'm rather confused as to whether something should be disposed of, or not.
            What is the general rule? How can you be sure of doing the right thing? I've
            heard about disposing unmanaged resources but what is an unmanaged resource?
            >
            Does everything need disposing?
            >
            I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
            sure of what needs disposing?
            >
            Do these get automatically get disposed of at the end of the procedure that
            declares them?
            >
            Is there any way to check my program to see if I have something that needs
            disposing and is still using resources?
            >
            -Jerry
            >
            There are two ways that you can choose which objects to dispose:

            1. If it has a Dispose method, call it.

            2. Read the documentation and possibly other sources to find out if the
            object really needs disposing. If you find reliable information that
            says that it's not needed, you can skip the Dispose call.

            Some people will say that the first is the only correct way, some will
            say that the second is. Both works, and you can choose the one that is
            suitable for your current situation. If you don't have time to waddle
            around in the docs, just call Dispose and be done with it.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Disposing objects

              Michel,

              Let me say it in another way:

              A lot of people write "You have to dispose everything in a class that
              Implements IDisposable.

              Which classes implements standard IDisposable? That are classes as forms as
              they are made with the designer, as you wrote already by the label,
              everything that is in the component is automaticly disposed.

              People who write "everything that implements Idisopsable" mostly mean (or
              it is understood), "Everything that has a dispose method".

              However a dispose method says nothing, you can create it in every class
              where that is not already there and which can be inherited, but by that does
              it not implement Idisposable.

              The standard dispose method (as it is inherited from component) is often
              overridden to release unmanaged resources as we see by instance in most
              Sharepoint classes. For sure I simple use everywhere in Sharepoint the
              dispose method as there classes are often not much more than Com wrappers
              and they have used Dispose for the releasing of that. So I agree that in
              Sharepoint it is good practise to use everywhere the Dispose method before
              something goes out of scope.

              However in AdoNet by instance there is nothing where the dispose method
              "should" be used. I could find also until now nowhere the dispose method in
              a Linq class.

              I have always a proplem when people in a kind of autheritair way write (I
              don't know if this is English word, but you understand it), that "dispose
              should be used wherever a class implements Idisopsable". They are in my idea
              just parroting, something I was not expecting from you.

              jmo

              :-)

              Cor






              "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
              news:%232X62hgJ JHA.5984@TK2MSF TNGP03.phx.gbl. ..
              Aha ,, well you wanted more detailed info Cor ;-) ?
              >
              As we both know that calling Dispose in that case is not necesary as
              Dispose is automatically called on all controls and components placed on
              windows forms or user controls.
              >
              However in general the rule is
              >
              dispose everything that implements IDisposable
              >
              >
              regards
              Michel
              >
              >
              >
              >
              >
              "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
              news:7428BB1B-7722-4BCD-A3BE-2CBF453FE12B@mi crosoft.com...
              >Michel,
              >>
              >Can you show me your latest program where you use the dispose method on
              >Label?
              >>
              >Cor
              >>
              >"Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
              >news:uDhDJMfJJ HA.468@TK2MSFTN GP06.phx.gbl...
              >>Every object that implements an idisposable interface should be disposed
              >>of
              >>i recomend you to write these objects in a using en using bloock
              >>>
              >>like this
              >>>
              >>Using Foo as new Foo.Lib
              >>>
              >>'''do your stuff
              >>>
              >>End using 'the end using will take care of disposing so no need to call
              >>it
              >>>
              >>also note that a declared variabel in a using block gets a block scope
              >>>
              >>Using Foo as new Foo.Lib
              >>Dim Bla As string 'only accessible in the using block
              >>'''do your stuff
              >>>
              >>End using
              >>>
              >>>
              >>>
              >>A unmanagaed resource is a resource that is not native to .Net but
              >>started through interop , PInvoke etc etc
              >>for instance API calls or COM objects
              >>>
              >>Object pointers that go out of scope will eventually be cleaned by the
              >>GC however how and when this happens is a mystery that only the GC knows
              >>:-)
              >>>
              >>hth
              >>>
              >>Michel Posseth [MCP]
              >>http://www.vbdotnetcoder.com
              >>>
              >>>
              >>>
              >>>
              >>>
              >>"Jerry Spence1" <123@dfgh.comsc hreef in bericht
              >>news:p9adnX Vn-LFgiXrVnZ2dnUVZ 8vqdnZ2d@posted .plusnet...
              >>>I'm rather confused as to whether something should be disposed of, or
              >>>not. What is the general rule? How can you be sure of doing the right
              >>>thing? I've heard about disposing unmanaged resources but what is an
              >>>unmanaged resource?
              >>>>
              >>>Does everything need disposing?
              >>>>
              >>>I've been using Webrequest, Webresponse, Streamreaders etc. How can I
              >>>be sure of what needs disposing?
              >>>>
              >>>Do these get automatically get disposed of at the end of the procedure
              >>>that declares them?
              >>>>
              >>>Is there any way to check my program to see if I have something that
              >>>needs disposing and is still using resources?
              >>>>
              >>>-Jerry
              >>>>
              >>>
              >>>
              >>
              >
              >

              Comment

              • Michel Posseth

                #8
                Re: Disposing objects

                IDisposable is a interface ( "Contract" ) , if the coder implemented
                IDisposable you may asume that there is a reasson why he implemented this
                "contract" in his code
                so seeing from that light you "should" call the dispose method

                Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.


                page 201 of the VB.Net core reference

                Because .Net objects don`t have real destructors , Well designed classes
                should expose a method to let well behaved clients manually release any
                resource such as files database connections and system objects as soon as
                they do not need them any longer -- that is , just before setting the
                reference to Nothing rather than waiting for for the subsequent garbage
                collection .

                Classes that want to provide this feature should implement IDisposable an
                interface defined in the .Net Framework ............... ..........

                page 202

                ..Net programming guidelines dictate that the dispose method of an object
                should invoke the dispose method of all the inner objects that the current
                object owns and that are hidden from the client code ......

                so yes it is an official guideline it is not MY guideline and to make it
                more interesting it is a guideline for all .Net languages


                And yes call me the dispose police but if i see code to objects in my
                company written that implement idisposable without using blocks i convert
                them to using blocks as i believe it is sloppy programming and for
                readability of the code
                if a contract is signed you must conform to that contract if you like it or
                not in real life and in the programming world .



                Michel Posseth [MCP]
                ASP.NET hosting, SQL hosting, AJAX Hosting, Silverlight hosting, LINQ Hosting, Microsoft Windows 2012 hosting, iis8 hosting, Windows 2012 R2 hosting, iis8.5 hosting.






                "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
                news:407A84E7-6629-4EF7-85E4-04E57F22EC6F@mi crosoft.com...
                Michel,
                >
                Let me say it in another way:
                >
                A lot of people write "You have to dispose everything in a class that
                Implements IDisposable.
                >
                Which classes implements standard IDisposable? That are classes as forms
                as they are made with the designer, as you wrote already by the label,
                everything that is in the component is automaticly disposed.
                >
                People who write "everything that implements Idisopsable" mostly mean (or
                it is understood), "Everything that has a dispose method".
                >
                However a dispose method says nothing, you can create it in every class
                where that is not already there and which can be inherited, but by that
                does it not implement Idisposable.
                >
                The standard dispose method (as it is inherited from component) is often
                overridden to release unmanaged resources as we see by instance in most
                Sharepoint classes. For sure I simple use everywhere in Sharepoint the
                dispose method as there classes are often not much more than Com wrappers
                and they have used Dispose for the releasing of that. So I agree that in
                Sharepoint it is good practise to use everywhere the Dispose method before
                something goes out of scope.
                >
                However in AdoNet by instance there is nothing where the dispose method
                "should" be used. I could find also until now nowhere the dispose method
                in a Linq class.
                >
                I have always a proplem when people in a kind of autheritair way write (I
                don't know if this is English word, but you understand it), that "dispose
                should be used wherever a class implements Idisopsable". They are in my
                idea just parroting, something I was not expecting from you.
                >
                jmo
                >
                :-)
                >
                Cor
                >
                >
                >
                >
                >
                >
                "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
                news:%232X62hgJ JHA.5984@TK2MSF TNGP03.phx.gbl. ..
                >Aha ,, well you wanted more detailed info Cor ;-) ?
                >>
                >As we both know that calling Dispose in that case is not necesary as
                >Dispose is automatically called on all controls and components placed on
                >windows forms or user controls.
                >>
                >However in general the rule is
                >>
                >dispose everything that implements IDisposable
                >>
                >>
                >regards
                >Michel
                >>
                >>
                >>
                >>
                >>
                >"Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
                >news:7428BB1 B-7722-4BCD-A3BE-2CBF453FE12B@mi crosoft.com...
                >>Michel,
                >>>
                >>Can you show me your latest program where you use the dispose method on
                >>Label?
                >>>
                >>Cor
                >>>
                >>"Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
                >>news:uDhDJMfJ JHA.468@TK2MSFT NGP06.phx.gbl.. .
                >>>Every object that implements an idisposable interface should be
                >>>disposed of
                >>>i recomend you to write these objects in a using en using bloock
                >>>>
                >>>like this
                >>>>
                >>>Using Foo as new Foo.Lib
                >>>>
                >>>'''do your stuff
                >>>>
                >>>End using 'the end using will take care of disposing so no need to
                >>>call it
                >>>>
                >>>also note that a declared variabel in a using block gets a block scope
                >>>>
                >>>Using Foo as new Foo.Lib
                >>>Dim Bla As string 'only accessible in the using block
                >>>'''do your stuff
                >>>>
                >>>End using
                >>>>
                >>>>
                >>>>
                >>>A unmanagaed resource is a resource that is not native to .Net but
                >>>started through interop , PInvoke etc etc
                >>>for instance API calls or COM objects
                >>>>
                >>>Object pointers that go out of scope will eventually be cleaned by the
                >>>GC however how and when this happens is a mystery that only the GC
                >>>knows :-)
                >>>>
                >>>hth
                >>>>
                >>>Michel Posseth [MCP]
                >>>http://www.vbdotnetcoder.com
                >>>>
                >>>>
                >>>>
                >>>>
                >>>>
                >>>"Jerry Spence1" <123@dfgh.comsc hreef in bericht
                >>>news:p9adnXV n-LFgiXrVnZ2dnUVZ 8vqdnZ2d@posted .plusnet...
                >>>>I'm rather confused as to whether something should be disposed of, or
                >>>>not. What is the general rule? How can you be sure of doing the right
                >>>>thing? I've heard about disposing unmanaged resources but what is an
                >>>>unmanaged resource?
                >>>>>
                >>>>Does everything need disposing?
                >>>>>
                >>>>I've been using Webrequest, Webresponse, Streamreaders etc. How can I
                >>>>be sure of what needs disposing?
                >>>>>
                >>>>Do these get automatically get disposed of at the end of the procedure
                >>>>that declares them?
                >>>>>
                >>>>Is there any way to check my program to see if I have something that
                >>>>needs disposing and is still using resources?
                >>>>>
                >>>>-Jerry
                >>>>>
                >>>>
                >>>>
                >>>
                >>
                >>
                >

                Comment

                • Michel Posseth

                  #9
                  Re: Disposing objects

                  The only thing i would like to add to this verry politicly correct answer of
                  Göran :-)


                  Failing to Dispose an object which implements IDisposable typically results
                  in the object going into the finalization queue (read Chapter 19 of Jeffrey
                  Richter's Applied Microsoft .NET Framework Programming for details). The
                  results of this is that an object's memory that might have otherwise been
                  freed in generation 01 be freed until a later generation collection. If
                  you're creating a lot of these objects, well, do the math."

                  So the worst thing that might happen is - that you should assume it could
                  cost you extra GC cycles if you don't ........


                  regards

                  And have a nice evening

                  Michel






                  "Göran Andersson" <guffa@guffa.co mschreef in bericht
                  news:eXrkBokJJH A.5984@TK2MSFTN GP03.phx.gbl...
                  Jerry Spence1 wrote:
                  >I'm rather confused as to whether something should be disposed of, or
                  >not. What is the general rule? How can you be sure of doing the right
                  >thing? I've heard about disposing unmanaged resources but what is an
                  >unmanaged resource?
                  >>
                  >Does everything need disposing?
                  >>
                  >I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
                  >sure of what needs disposing?
                  >>
                  >Do these get automatically get disposed of at the end of the procedure
                  >that declares them?
                  >>
                  >Is there any way to check my program to see if I have something that
                  >needs disposing and is still using resources?
                  >>
                  >-Jerry
                  >
                  There are two ways that you can choose which objects to dispose:
                  >
                  1. If it has a Dispose method, call it.
                  >
                  2. Read the documentation and possibly other sources to find out if the
                  object really needs disposing. If you find reliable information that says
                  that it's not needed, you can skip the Dispose call.
                  >
                  Some people will say that the first is the only correct way, some will say
                  that the second is. Both works, and you can choose the one that is
                  suitable for your current situation. If you don't have time to waddle
                  around in the docs, just call Dispose and be done with it.
                  >
                  --
                  Göran Andersson
                  _____
                  http://www.guffa.com

                  Comment

                  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                    #10
                    Re: Disposing objects

                    Michel Posseth wrote:
                    The only thing i would like to add to this verry politicly correct
                    answer of Göran :-)
                    >
                    >
                    Failing to Dispose an object which implements IDisposable typically
                    results in the object going into the finalization queue (read Chapter 19
                    of Jeffrey Richter's Applied Microsoft .NET Framework Programming for
                    details). The results of this is that an object's memory that might have
                    otherwise been freed in generation 01 be freed until a later generation
                    collection. If you're creating a lot of these objects, well, do the math."
                    >
                    So the worst thing that might happen is - that you should assume it
                    could cost you extra GC cycles if you don't ........
                    >
                    >
                    regards
                    >
                    And have a nice evening
                    >
                    Michel
                    >
                    Good point. However, it only applies if the class actually has a finalizer.

                    So, if the class has a Dispose method and a finalizer, you should always
                    call the Dispose method.


                    --
                    Göran Andersson
                    _____
                    Göran Anderssons privata hemsida.

                    Comment

                    • Brian Gideon

                      #11
                      Re: Disposing objects

                      On Oct 4, 3:31 am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                      wrote:
                      Michel,
                      >
                      Can you show me your latest program where you use the dispose method on
                      Label?
                      >
                      Cor
                      Controls are disposed automatically so it would not be difficult to
                      satisfy that request. In fact, you could probably just pull up a few
                      of your own.

                      Comment

                      • Brian Gideon

                        #12
                        Re: Disposing objects

                        On Oct 4, 1:23 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                        wrote:
                        Michel,
                        >
                        Let me say it in another way:
                        >
                        A lot of people write "You have to dispose everything in a class that
                        Implements IDisposable.
                        I am in that camp.
                        >
                        Which classes implements standard IDisposable? That are classes as forms as
                        they are made with the designer, as you wrote already by the label,
                        everything that is in the component is automaticly disposed.
                        >
                        People who write "everything that implements Idisopsable"  mostly mean (or
                        it is understood), "Everything that has a dispose method".
                        >
                        However a dispose method says nothing, you can create it in every class
                        where that is not already there and which can be inherited, but by that does
                        it not implement Idisposable.
                        When I'm talking about calling Dispose I'm specifically referring to
                        classes that implement IDisposable. I'm not aware of any classes that
                        have a Dispose method without implementing IDisposable; at least in
                        the BCL.
                        >
                        The standard dispose method (as it is inherited from component) is often
                        overridden to release unmanaged resources as we see by instance in most
                        Sharepoint classes. For sure I simple use everywhere in Sharepoint the
                        dispose method as there classes are often not much more than Com wrappers
                        and they have used Dispose for the releasing of that. So I agree that in
                        Sharepoint it is good practise to use everywhere the Dispose method before
                        something goes out of scope.
                        >
                        Just to clarify, the standard Dispose method is not marked as virtual
                        so it actually cannot be overriden. However, the protected
                        Dispose(Boolean ) can be overriden.
                        However in AdoNet by instance there is nothing where the dispose method
                        "should" be used. I could find also until now nowhere the dispose method in
                        a Linq class.
                        >
                        Do you understand the consequences of not calling Dispose on say...a
                        SqlConnection instance?
                        I have always a proplem when people in a kind of  autheritair way write(I
                        don't know if this is English word, but you understand it), that "dispose
                        should be used wherever a class implements Idisopsable". They are in my idea
                        just parroting, something I was not expecting from you.
                        >
                        I happen to put a lot of stock in the MSDN documentation myself.

                        Comment

                        • Brian Gideon

                          #13
                          Re: Disposing objects

                          On Oct 4, 12:52 pm, Göran Andersson <gu...@guffa.co mwrote:
                          Jerry Spence1 wrote:
                          I'm rather confused as to whether something should be disposed of, or not.
                          What is the general rule? How can you be sure of doing the right thing?I've
                          heard about disposing unmanaged resources but what is an unmanaged resource?
                          >
                          Does everything need disposing?
                          >
                          I've been using Webrequest, Webresponse, Streamreaders etc. How can I be
                          sure of what needs disposing?
                          >
                          Do these get automatically get disposed of at the end of the procedure that
                          declares them?
                          >
                          Is there any way to check my program to see if I have something that needs
                          disposing and is still using resources?
                          >
                          -Jerry
                          >
                          There are two ways that you can choose which objects to dispose:
                          >
                          1. If it has a Dispose method, call it.
                          >
                          2. Read the documentation and possibly other sources to find out if the
                          object really needs disposing. If you find reliable information that
                          says that it's not needed, you can skip the Dispose call.
                          >
                          Some people will say that the first is the only correct way, some will
                          say that the second is. Both works, and you can choose the one that is
                          suitable for your current situation. If you don't have time to waddle
                          around in the docs, just call Dispose and be done with it.
                          >
                          I like to sum it up this way. When in doubt call Dispose. That
                          usually leads me to preferring #1.

                          Comment

                          • Cor Ligthert[MVP]

                            #14
                            Re: Disposing objects

                            >Do you understand the consequences of not calling Dispose on say...a
                            >SqlConnectio n instance?



                            Cor

                            Comment

                            • Brian Gideon

                              #15
                              Re: Disposing objects

                              On Oct 5, 10:59 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                              wrote:
                              Do you understand the consequences of not calling Dispose on say...a
                              SqlConnection instance?
                              >
                              http://groups.google.com/group/micro...framework.adon...
                              >
                              Cor
                              Yeah, Close and Dispose both close the connection. Everybody knows
                              that. So calling Close is the next best thing in lieu of calling
                              Dispose. That's great.

                              What other benefits do you miss out on by not calling Dispose? What
                              other unmanaged resources are left dangling until the GC runs the
                              finalizer? By analyzing with Reflector I can see that you'd only be
                              limited to keeping the object on the finalization queue...right now
                              anyway.

                              What would happen if Microsoft decided to tweak the implementation so
                              that Dispose did more things in a future version?

                              Now take a look at OleDbConnection using Reflector. Dispose really is
                              doing more than just closing the connection in that case. What about
                              a proprietary IDbConnection?

                              I'm currently working on a native .NET managed provider for a
                              proprietary database. I can tell you that Dispose is more aggressive
                              in releasing unmanaged resources than Close in my implementation.

                              Comment

                              Working...