What has managed code achieved?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wolfgang Enzinger

    #61
    Re: What has managed code achieved?

    On Fri, 24 Oct 2008 14:01:58 -0500, "Alex Clark" wrote:
    >Just to clarify, are you saying that in VB6 if you didn't de-reference an
    >object before it went out of scope, it would automatically be cleaned up
    >anyway? I'm not saying this in an accusatory manner, but everything I ever
    >read in terms of good practice and even articles on MSDN seemed to imply
    >otherwise - in fact I can even remember it being touted as a selling point
    >to upgrade to VB.NET.
    >
    >Example (VB6):
    >
    >Private Sub Test()
    >
    Dim objRef As SomeClass
    Set objRef = New SomeClass
    ' ...
    ' ...
    Set objRef = Nothing ' <--- Dereferencing before it goes out of scope
    >
    >End Sub
    >
    >Are you saying that the last line, "Set objRef = Nothing", was therefore
    >unneccesary in VB6?
    I think that this is what Ben is saying, and I can confirm that it is
    absolutely true. The above "Set objRef = Nothing" is like setting a
    string to vbNullString before leaving a sub/function ... it frees
    memory but that would happen automatically anyway.

    Apart from API calls handled improperly, the only way to produce those
    memory leaks that VB is allegedly well known for is to have unresolved
    circular object references.
    >[...] then apparently VB6 was a bit smarter than I gave it credit for!
    Hehe! :-)

    Wolfgang

    Comment

    • Herfried K. Wagner [MVP]

      #62
      Re: What has managed code achieved?

      "Alex Clark" <quanta@noemail .noemailschrieb :
      Just to clarify, are you saying that in VB6 if you didn't de-reference an
      object before it went out of scope, it would automatically be cleaned up
      anyway? I'm not saying this in an accusatory manner, but everything I
      ever read in terms of good practice and even articles on MSDN seemed to
      imply otherwise - in fact I can even remember it being touted as a selling
      point to upgrade to VB.NET.
      >
      Example (VB6):
      >
      Private Sub Test()
      >
      Dim objRef As SomeClass
      Set objRef = New SomeClass
      ' ...
      ' ...
      Set objRef = Nothing ' <--- Dereferencing before it goes out of scope
      >
      End Sub
      >
      Are you saying that the last line, "Set objRef = Nothing", was therefore
      unneccesary in VB6? I was always under the impression that memory leaks
      courtesy of objects that are still referenced (but out of scope) would
      occur if you did *not* de-reference before they went out of scope
      No, the reference stored in the local variable would have been removed
      automatically. However, there were some pitfalls: Jumping out of a 'With'
      block, for example, led to a reference which could not be removed but
      prevented the object from being destroyed:

      \\\
      Private Declare Sub CopyMemory _
      Lib "Kernel32.d ll" _
      Alias "RtlMoveMem ory" _
      ( _
      ByRef Destination As Any, _
      ByRef Source As Any, _
      ByVal Length As Long _
      )

      Private Sub Form_Click()
      Dim f As Form1
      Set f = New Form1
      Call MsgBox(GetRefCo unt(f)) ' 1.

      ' Increments reference count.
      With f

      ' Jumping out of the block does not decrement the
      ' reference count.
      GoTo Label

      ' Decrements reference count.
      End With
      Label:
      Call MsgBox(GetRefCo unt(f)) ' 2.
      End Sub

      Private Function GetRefCount(ByR ef Object As IUnknown) As Long
      Call CopyMemory(GetR efCount, ByVal ObjPtr(Object) + 4, 4)
      GetRefCount = GetRefCount - 2
      End Function
      ///

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

      Comment

      • Cor Ligthert[MVP]

        #63
        Re: What has managed code achieved?

        Ok, let me than complete what you did not say.

        An object is removed as soon as it has no existing reference anymore itself,
        or that its reference is used in another object (not being the object
        itself).

        It can have a reference in a Module or the Class is Shared (static in C#),
        Global or in a method. His own reference will set to nothing: as method
        goes out of scope, as its object class goes out of scope "me" ("this" in C#)
        or as soon as the program stops; or as a reference is set to another object.

        But that is not everything, it will not be removed as long as another
        something is pointing to the object, while that object itself is not an
        object in the list of that.

        An Example.

        Drag a datagridview on your form, it will be set in the global part of your
        form
        Create a datatable with a row and add that to your datatable row collection.
        Set that datatable as the dataset of your grid

        Now the datatable will not be removed as long as you don't set the
        dataSource of the datagridview to nothing or to another datatable.

        This is an example as we have often seen here, where people thought it went
        wrong (a bug) because they did not understand this.

        Cor




        >
        That's not what I said.
        >
        Consider the objects A, B, and C, and -denoting a reference:
        >
        A -B -C
        >
        If A can be reached from the "running code", B and C cannot be finalized.
        >
        However, when removing the reference from A to B
        >
        A B -C
        >

        Comment

        • Wolfgang Enzinger

          #64
          Re: What has managed code achieved?

          On Sat, 25 Oct 2008 03:47:55 +0200, "Herfried K. Wagner [MVP]" wrote:
          >No, the reference stored in the local variable would have been removed
          >automaticall y. However, there were some pitfalls: Jumping out of a 'With'
          >block, for example, led to a reference which could not be removed but
          >prevented the object from being destroyed:
          Good point, forgot to mention this.

          Jumping out of a With block is generally dangerous as can be seen
          here:

          '***
          Option Explicit

          Private Type TestType
          Value As Long
          End Type

          Sub main()
          Dim tt() As TestType
          Dim l As Long
          ReDim tt(2)
          l = 0
          Do
          With tt(l)
          .Value = l
          If .Value = 2 Then Exit Do 'jumps out of block
          End With
          l = l + 1
          Loop
          ReDim Preserve tt(1) '->fails with RTE 10, array is locked
          End Sub
          '***

          However, you were talking about *some* pitfalls and jumping out of a
          With block as an *example*. Which other situations that lead to
          omitted dereferencing do you have in mind?

          Wolfgang

          Comment

          • Herfried K. Wagner [MVP]

            #65
            Re: What has managed code achieved?

            "Wolfgang Enzinger" <weusenet@tempo raryforwarding. comschrieb:
            However, you were talking about *some* pitfalls and jumping out of a
            With block as an *example*. Which other situations that lead to
            omitted dereferencing do you have in mind?
            Well, I only had the particular scenario I showed in my post in my mind, but
            I was thinking about different reasons for jumping out of a 'With' block.

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

            Comment

            • William Vaughn \(MVP\)

              #66
              Re: What has managed code achieved?

              I asked around and got this response:
              Most "serious" apps that Microsoft ships (including the OS) has a mix of
              both .NET managed code and COM/ASM code--some more than others. Some are
              nearly all .NET while others are built to use COM and need to be implemented
              with COM.

              --
              _______________ _______________ _______________ _______________ ______________
              William R. Vaughn
              President and Founder Beta V Corporation
              Author, Mentor, Dad, Grandpa
              Microsoft MVP
              (425) 556-9205 (Pacific time)
              Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
              _______________ _______________ _______________ _______________ _______________ _______________ __



              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
              news:uva2NDKOJH A.3636@TK2MSFTN GP05.phx.gbl...
              "Wolfgang Enzinger" <weusenet@tempo raryforwarding. comschrieb:
              >However, you were talking about *some* pitfalls and jumping out of a
              >With block as an *example*. Which other situations that lead to
              >omitted dereferencing do you have in mind?
              >
              Well, I only had the particular scenario I showed in my post in my mind,
              but I was thinking about different reasons for jumping out of a 'With'
              block.
              >
              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

              Comment

              • Cor Ligthert[MVP]

                #67
                Re: What has managed code achieved?

                Hi Bill,

                I get the idea that you are answering a message from somebody in a newsgroup
                which has removed some groups in the grouplist.

                In the VB language group it shows up as an answer to Herfried, but it has in
                my idea not anything to do with his message.

                Cor

                "William Vaughn (MVP)" <billva@NoSpamB etav.comwrote in message
                news:%2368AlJiO JHA.2312@TK2MSF TNGP03.phx.gbl. ..
                >I asked around and got this response:
                Most "serious" apps that Microsoft ships (including the OS) has a mix of
                both .NET managed code and COM/ASM code--some more than others. Some are
                nearly all .NET while others are built to use COM and need to be
                implemented with COM.
                >
                --
                _______________ _______________ _______________ _______________ ______________
                William R. Vaughn
                President and Founder Beta V Corporation
                Author, Mentor, Dad, Grandpa
                Microsoft MVP
                (425) 556-9205 (Pacific time)
                Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
                _______________ _______________ _______________ _______________ _______________ _______________ __
                >
                >
                >
                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                news:uva2NDKOJH A.3636@TK2MSFTN GP05.phx.gbl...
                >"Wolfgang Enzinger" <weusenet@tempo raryforwarding. comschrieb:
                >>However, you were talking about *some* pitfalls and jumping out of a
                >>With block as an *example*. Which other situations that lead to
                >>omitted dereferencing do you have in mind?
                >>
                >Well, I only had the particular scenario I showed in my post in my mind,
                >but I was thinking about different reasons for jumping out of a 'With'
                >block.
                >>
                >--
                >M S Herfried K. Wagner
                >M V P <URL:http://dotnet.mvps.org/>
                >V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                >

                Comment

                Working...