Dispose then set to nothing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SGFvIEppYW5n?=

    #1

    Dispose then set to nothing

    Hi,

    I see in many MS example code that looks like this

    Public Sub Foo()
    dim myCustomer as New Customer
    ...
    myCustomer.Disp ose()
    myCustomer = Nothing
    End Sub

    I'm wondering if setting myCustomer = Nothing is necessary in this case?
    Since myCustomer is a private variable, once it goes out of scope at the next
    line, the reference to the object should be released.

    Thanks.

    Hao
  • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

    #2
    Re: Dispose then set to nothing

    Hao Jiang wrote:
    Hi,
    >
    I see in many MS example code that looks like this
    >
    Public Sub Foo()
    dim myCustomer as New Customer
    ...
    myCustomer.Disp ose()
    myCustomer = Nothing
    End Sub
    >
    I'm wondering if setting myCustomer = Nothing is necessary in this case?
    Since myCustomer is a private variable, once it goes out of scope at the next
    line, the reference to the object should be released.
    >
    Thanks.
    >
    Hao
    That is correct. Removing the reference is pointless in this case.

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

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Dispose then set to nothing

      "Hao Jiang" <Hao Jiang@discussio ns.microsoft.co mschrieb:
      Public Sub Foo()
      dim myCustomer as New Customer
      ...
      myCustomer.Disp ose()
      myCustomer = Nothing
      End Sub
      >
      I'm wondering if setting myCustomer = Nothing is necessary in this case?
      No, it actually isn't necessary for the reason you mentioned.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Dispose then set to nothing

        Hao Jiang,
        As Göran & Herfried suggests setting myCustomer = Nothing is not needed for
        the reason you give.

        Instead of calling Dispose outright I would recommend using the new Using
        statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose is
        called even if one of the statements contained by the Using throws an
        exception.
        Public Sub Foo()
        Using myCustomer as New Customer
        ...
        End Using
        End Sub
        Which is basically the following in .NET 1.x:

        Dim myCustomer As New Customer
        Try
        DoSomething()
        Finally
        If myCustomer IsNot Nothing Then
        myCustomer.Disp ose()
        End If
        End Try

        --
        Hope this helps
        Jay B. Harlow [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        "Hao Jiang" <Hao Jiang@discussio ns.microsoft.co mwrote in message
        news:4551F173-DAB8-4F74-AE61-9F4298362B17@mi crosoft.com...
        Hi,
        >
        I see in many MS example code that looks like this
        >
        Public Sub Foo()
        dim myCustomer as New Customer
        ...
        myCustomer.Disp ose()
        myCustomer = Nothing
        End Sub
        >
        I'm wondering if setting myCustomer = Nothing is necessary in this case?
        Since myCustomer is a private variable, once it goes out of scope at the
        next
        line, the reference to the object should be released.
        >
        Thanks.
        >
        Hao

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Dispose then set to nothing

          Hao Jiang,

          Do you mean with MS samples, samples made by Micrsoft, if so can you than
          show us some of those?
          (URL's)

          Because it is not correct, it is as
          dim a as integer = 1 + 1
          if a <2 then messagebox.show "there was a calculation error"

          Thanks in advance,

          Cor

          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.nets chreef in
          bericht news:eItOnFiWHH A.4860@TK2MSFTN GP04.phx.gbl...
          Hao Jiang,
          As Göran & Herfried suggests setting myCustomer = Nothing is not needed
          for the reason you give.
          >
          Instead of calling Dispose outright I would recommend using the new Using
          statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose
          is called even if one of the statements contained by the Using throws an
          exception.
          >
          >Public Sub Foo()
          Using myCustomer as New Customer
          > ...
          End Using
          >End Sub
          >
          Which is basically the following in .NET 1.x:
          >
          Dim myCustomer As New Customer
          Try
          DoSomething()
          Finally
          If myCustomer IsNot Nothing Then
          myCustomer.Disp ose()
          End If
          End Try
          >
          --
          Hope this helps
          Jay B. Harlow [MVP - Outlook]
          .NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net
          >
          >
          "Hao Jiang" <Hao Jiang@discussio ns.microsoft.co mwrote in message
          news:4551F173-DAB8-4F74-AE61-9F4298362B17@mi crosoft.com...
          >Hi,
          >>
          >I see in many MS example code that looks like this
          >>
          >Public Sub Foo()
          > dim myCustomer as New Customer
          > ...
          > myCustomer.Disp ose()
          > myCustomer = Nothing
          >End Sub
          >>
          >I'm wondering if setting myCustomer = Nothing is necessary in this case?
          >Since myCustomer is a private variable, once it goes out of scope at the
          >next
          >line, the reference to the object should be released.
          >>
          >Thanks.
          >>
          >Hao
          >

          Comment

          • aaron.kempf@gmail.com

            #6
            Re: Dispose then set to nothing

            yeah; in vb6 when a variable went out of scope; it automagically
            cleaned up after itself


            except for DAO-- (which MS just ressurrected)









            On Feb 26, 3:30 pm, Hao Jiang <Hao J...@discussion s.microsoft.com >
            wrote:
            Hi,
            >
            I see in many MS example code that looks like this
            >
            Public Sub Foo()
            dim myCustomer as New Customer
            ...
            myCustomer.Disp ose()
            myCustomer = Nothing
            End Sub
            >
            I'm wondering if setting myCustomer = Nothing is necessary in this case?
            Since myCustomer is a private variable, once it goes out of scope at the next
            line, the reference to the object should be released.
            >
            Thanks.
            >
            Hao

            Comment

            • =?Utf-8?B?SGFvIEppYW5n?=

              #7
              Re: Dispose then set to nothing

              Thanks Goran, Hefried and Jay! That's what I thought.

              Cor, please check out the 2nd and 3rd code section on this MS page:


              Hao

              "Cor Ligthert [MVP]" wrote:
              Hao Jiang,
              >
              Do you mean with MS samples, samples made by Micrsoft, if so can you than
              show us some of those?
              (URL's)
              >
              Because it is not correct, it is as
              dim a as integer = 1 + 1
              if a <2 then messagebox.show "there was a calculation error"
              >
              Thanks in advance,
              >
              Cor
              >
              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.nets chreef in
              bericht news:eItOnFiWHH A.4860@TK2MSFTN GP04.phx.gbl...
              Hao Jiang,
              As Göran & Herfried suggests setting myCustomer = Nothing is not needed
              for the reason you give.

              Instead of calling Dispose outright I would recommend using the new Using
              statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose
              is called even if one of the statements contained by the Using throws an
              exception.
              Public Sub Foo()
              Using myCustomer as New Customer
              ...
              End Using
              End Sub
              Which is basically the following in .NET 1.x:

              Dim myCustomer As New Customer
              Try
              DoSomething()
              Finally
              If myCustomer IsNot Nothing Then
              myCustomer.Disp ose()
              End If
              End Try

              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              .NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "Hao Jiang" <Hao Jiang@discussio ns.microsoft.co mwrote in message
              news:4551F173-DAB8-4F74-AE61-9F4298362B17@mi crosoft.com...
              Hi,
              >
              I see in many MS example code that looks like this
              >
              Public Sub Foo()
              dim myCustomer as New Customer
              ...
              myCustomer.Disp ose()
              myCustomer = Nothing
              End Sub
              >
              I'm wondering if setting myCustomer = Nothing is necessary in this case?
              Since myCustomer is a private variable, once it goes out of scope at the
              next
              line, the reference to the object should be released.
              >
              Thanks.
              >
              Hao
              >
              >
              >

              Comment

              • Brian Gideon

                #8
                Re: Dispose then set to nothing

                On Feb 26, 5:30 pm, Hao Jiang <Hao J...@discussion s.microsoft.com >
                wrote:
                Hi,
                >
                I see in many MS example code that looks like this
                >
                Public Sub Foo()
                dim myCustomer as New Customer
                ...
                myCustomer.Disp ose()
                myCustomer = Nothing
                End Sub
                >
                I'm wondering if setting myCustomer = Nothing is necessary in this case?
                Since myCustomer is a private variable, once it goes out of scope at the next
                line, the reference to the object should be released.
                >
                Thanks.
                >
                Hao
                Hao,

                To be absolutely precise the object is eligible for garbage collection
                even before it goes out of scope as long as it's not used anymore. I
                don't know how aggressive the GC rules are specifically, but it is
                possible that the GC would consider the line myCustomer = Nothing as
                having no side effects on the object and would collect the object even
                before the line executed. I suppose it's reasonable to theorize that
                the JIT compiler could optimize the line away as well.

                Brian

                Brian


                Comment

                • Brian Gideon

                  #9
                  Re: Dispose then set to nothing

                  On Feb 27, 11:25 am, Hao Jiang <HaoJi...@discu ssions.microsof t.com>
                  wrote:
                  Thanks Goran, Hefried and Jay! That's what I thought.
                  >
                  Cor, please check out the 2nd and 3rd code section on this MS page:http://support.microsoft.com/kb/888168
                  >
                  Hao
                  Hmm...yeah, those are poorly written examples.

                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Dispose then set to nothing

                    Hao,

                    Thanks, I have sent it further

                    Cor

                    "Hao Jiang" <HaoJiang@discu ssions.microsof t.comschreef in bericht
                    news:FAA14219-133E-44F6-A858-B27760C9955B@mi crosoft.com...
                    Thanks Goran, Hefried and Jay! That's what I thought.
                    >
                    Cor, please check out the 2nd and 3rd code section on this MS page:

                    >
                    Hao
                    >
                    "Cor Ligthert [MVP]" wrote:
                    >
                    >Hao Jiang,
                    >>
                    >Do you mean with MS samples, samples made by Micrsoft, if so can you than
                    >show us some of those?
                    >(URL's)
                    >>
                    >Because it is not correct, it is as
                    >dim a as integer = 1 + 1
                    >if a <2 then messagebox.show "there was a calculation error"
                    >>
                    >Thanks in advance,
                    >>
                    >Cor
                    >>
                    >"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.nets chreef in
                    >bericht news:eItOnFiWHH A.4860@TK2MSFTN GP04.phx.gbl...
                    Hao Jiang,
                    As Göran & Herfried suggests setting myCustomer = Nothing is not needed
                    for the reason you give.
                    >
                    Instead of calling Dispose outright I would recommend using the new
                    Using
                    statement in .NET 2.0 (VS 2005). The Using statement ensures that
                    Dispose
                    is called even if one of the statements contained by the Using throws
                    an
                    exception.
                    >
                    >Public Sub Foo()
                    Using myCustomer as New Customer
                    > ...
                    End Using
                    >End Sub
                    >
                    Which is basically the following in .NET 1.x:
                    >
                    Dim myCustomer As New Customer
                    Try
                    DoSomething()
                    Finally
                    If myCustomer IsNot Nothing Then
                    myCustomer.Disp ose()
                    End If
                    End Try
                    >
                    --
                    Hope this helps
                    Jay B. Harlow [MVP - Outlook]
                    .NET Application Architect, Enthusiast, & Evangelist
                    T.S. Bradley - http://www.tsbradley.net
                    >
                    >
                    "Hao Jiang" <Hao Jiang@discussio ns.microsoft.co mwrote in message
                    news:4551F173-DAB8-4F74-AE61-9F4298362B17@mi crosoft.com...
                    >Hi,
                    >>
                    >I see in many MS example code that looks like this
                    >>
                    >Public Sub Foo()
                    > dim myCustomer as New Customer
                    > ...
                    > myCustomer.Disp ose()
                    > myCustomer = Nothing
                    >End Sub
                    >>
                    >I'm wondering if setting myCustomer = Nothing is necessary in this
                    >case?
                    >Since myCustomer is a private variable, once it goes out of scope at
                    >the
                    >next
                    >line, the reference to the object should be released.
                    >>
                    >Thanks.
                    >>
                    >Hao
                    >
                    >>
                    >>
                    >>

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: Dispose then set to nothing

                      Brian,

                      It is completely inside the method, if the dispose is not there it goes out
                      of scoop and will be disposed because the method ends.

                      In my idea it is completely out of sense. (Not that I have the idea that you
                      have an opposite opinion).

                      Cor

                      "Brian Gideon" <briangideon@ya hoo.comschreef in bericht
                      news:1172597399 .940193.16580@h 3g2000cwc.googl egroups.com...
                      On Feb 26, 5:30 pm, Hao Jiang <Hao J...@discussion s.microsoft.com >
                      wrote:
                      >Hi,
                      >>
                      >I see in many MS example code that looks like this
                      >>
                      >Public Sub Foo()
                      > dim myCustomer as New Customer
                      > ...
                      > myCustomer.Disp ose()
                      > myCustomer = Nothing
                      >End Sub
                      >>
                      >I'm wondering if setting myCustomer = Nothing is necessary in this case?
                      >Since myCustomer is a private variable, once it goes out of scope at the
                      >next
                      >line, the reference to the object should be released.
                      >>
                      >Thanks.
                      >>
                      >Hao
                      >
                      Hao,
                      >
                      To be absolutely precise the object is eligible for garbage collection
                      even before it goes out of scope as long as it's not used anymore. I
                      don't know how aggressive the GC rules are specifically, but it is
                      possible that the GC would consider the line myCustomer = Nothing as
                      having no side effects on the object and would collect the object even
                      before the line executed. I suppose it's reasonable to theorize that
                      the JIT compiler could optimize the line away as well.
                      >
                      Brian
                      >
                      Brian
                      >
                      >

                      Comment

                      • Brian Gideon

                        #12
                        Re: Dispose then set to nothing

                        On Feb 27, 12:00 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                        wrote:
                        Brian,
                        >
                        It is completely inside the method, if the dispose is not there it goes out
                        of scoop and will be disposed because the method ends.
                        >
                        In my idea it is completely out of sense. (Not that I have the idea that you
                        have an opposite opinion).
                        >
                        Cor
                        I'm not seeing how that's relevant. What I'm saying is that the
                        object is eligible for collection before the method returns and
                        possibly before it's variable reference is set to Nothing. And if
                        that's not true now then there's nothing in the CLI specification that
                        says a future version can't be that aggressive. Not only is setting
                        the reference to Nothing unnecessary, it could actually occur *after*
                        the memory has been released. That's something to consider in the
                        context of the OP's question.

                        Comment

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

                          #13
                          Re: Dispose then set to nothing

                          Cor Ligthert [MVP] wrote:
                          It is completely inside the method, if the dispose is not there it goes out
                          of scoop and will be disposed because the method ends.
                          Just to be sure that it doesn't add to the confusion:

                          Removing the reference to an object and disposing an object are two
                          completely different things.

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

                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: Dispose then set to nothing

                            Removing the reference to an object and disposing an object are two
                            completely different things.
                            Yes going to the toilet and flush the toilet too, but here is an automatic
                            flushing system, a little bit strange to do it twice.

                            Cor,


                            "Göran Andersson" <guffa@guffa.co mschreef in bericht
                            news:%23S2Em%23 pWHHA.392@TK2MS FTNGP06.phx.gbl ...
                            Cor Ligthert [MVP] wrote:
                            >It is completely inside the method, if the dispose is not there it goes
                            >out of scoop and will be disposed because the method ends.
                            >
                            Just to be sure that it doesn't add to the confusion:
                            >
                            Removing the reference to an object and disposing an object are two
                            completely different things.
                            >
                            --
                            Göran Andersson
                            _____
                            http://www.guffa.com

                            Comment

                            • Brian Gideon

                              #15
                              Re: Dispose then set to nothing

                              On Feb 27, 3:17 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                              wrote:
                              Yes going to the toilet and flush the toilet too, but here is an automatic
                              flushing system, a little bit strange to do it twice.
                              >
                              Cor,
                              >
                              Cor,

                              I'm apologize in advance, I'm having a hard time understanding what
                              you're saying. How does your analogy relate to .NET. What's
                              happening twice? And how does the concept of disposing an object
                              relate to the OP's question?

                              To me anyway, the fact that the OP's question just happened to involve
                              an object that implements IDisposable was completely irrelevant.

                              Brian

                              Comment

                              Working...