Idisposable

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

    Idisposable

    VB.Net Documentation for implementing IDisposable has:

    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    ' Free other state (managed objects).
    End If
    Tag.Dispose()
    ' Free your own state (unmanaged objects).
    ' Set large fields to null.
    End Sub

    I am having trouble understanding the comments:
    What does "state (managed objects)" mean
    What does "state (unmanaged objects)" mean
    I understand managed objects and unmanaged objects like com objects, certain
    GDI objects are unmanaged and should be disposed here. But what does the
    "state" mean?

    --
    Dennis in Houston
  • Scott M.

    #2
    Re: Idisposable

    It just means that the object would free itself from whatever resources it
    was holding, thus it would no longer be statefull. Don't let the wording
    hang you up.


    "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
    news:769C3CE5-6324-4156-8D49-ED72BC706AAD@mi crosoft.com...[color=blue]
    > VB.Net Documentation for implementing IDisposable has:
    >
    > Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
    > If disposing Then
    > ' Free other state (managed objects).
    > End If
    > Tag.Dispose()
    > ' Free your own state (unmanaged objects).
    > ' Set large fields to null.
    > End Sub
    >
    > I am having trouble understanding the comments:
    > What does "state (managed objects)" mean
    > What does "state (unmanaged objects)" mean
    > I understand managed objects and unmanaged objects like com objects,
    > certain
    > GDI objects are unmanaged and should be disposed here. But what does the
    > "state" mean?
    >
    > --
    > Dennis in Houston[/color]


    Comment

    • Dennis

      #3
      Re: Idisposable

      Thanks. I guess the State just means that the instance of it exists and the
      managed objects will free themselves (sometime) whereas I have to free all my
      own state unmanaged objects like the GDI pens, etc.

      "Scott M." wrote:
      [color=blue]
      > It just means that the object would free itself from whatever resources it
      > was holding, thus it would no longer be statefull. Don't let the wording
      > hang you up.
      >
      >
      > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
      > news:769C3CE5-6324-4156-8D49-ED72BC706AAD@mi crosoft.com...[color=green]
      > > VB.Net Documentation for implementing IDisposable has:
      > >
      > > Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
      > > If disposing Then
      > > ' Free other state (managed objects).
      > > End If
      > > Tag.Dispose()
      > > ' Free your own state (unmanaged objects).
      > > ' Set large fields to null.
      > > End Sub
      > >
      > > I am having trouble understanding the comments:
      > > What does "state (managed objects)" mean
      > > What does "state (unmanaged objects)" mean
      > > I understand managed objects and unmanaged objects like com objects,
      > > certain
      > > GDI objects are unmanaged and should be disposed here. But what does the
      > > "state" mean?
      > >
      > > --
      > > Dennis in Houston[/color]
      >
      >
      >[/color]

      Comment

      • Dennis

        #4
        RE: Idisposable

        Scott, it would be very helpful if you could give me an example of :

        a State Managed Object and a "your own State unmanaged object. For example,
        is a GDI Pen "state managed object" or my own state managed object"

        I really appreciate you helping me to understand this.

        "Dennis" wrote:
        [color=blue]
        > VB.Net Documentation for implementing IDisposable has:
        >
        > Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
        > If disposing Then
        > ' Free other state (managed objects).
        > End If
        > Tag.Dispose()
        > ' Free your own state (unmanaged objects).
        > ' Set large fields to null.
        > End Sub
        >
        > I am having trouble understanding the comments:
        > What does "state (managed objects)" mean
        > What does "state (unmanaged objects)" mean
        > I understand managed objects and unmanaged objects like com objects, certain
        > GDI objects are unmanaged and should be disposed here. But what does the
        > "state" mean?
        >
        > --
        > Dennis in Houston[/color]

        Comment

        • Scott M.

          #5
          Re: Idisposable

          Hi Dennis,

          I really don't know what a GDI Pen object is (I have no experience with it.
          Having said that, it's really pretty simple...manage d objects are classes
          that are instanced from and their code is run 100% by the Common Language
          Runtime. Many of the classes in the .NET Framework are 100% managed
          objects. Some however, are not. An OLEDBConnection is not 100% managed
          because the underlying OLEDB Provider is COM based.

          An unmanaged object is a class that is NOT instanced by the CLR. So, any
          non .NET objects (COM for example) would qualify here. Examples of
          unmanaged objects would be any ActiveX control or other COM objects like
          automating Office classes.


          "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
          news:346D7D96-BBB2-4AD9-A523-5C46A48173E1@mi crosoft.com...[color=blue]
          > Scott, it would be very helpful if you could give me an example of :
          >
          > a State Managed Object and a "your own State unmanaged object. For
          > example,
          > is a GDI Pen "state managed object" or my own state managed object"
          >
          > I really appreciate you helping me to understand this.
          >
          > "Dennis" wrote:
          >[color=green]
          >> VB.Net Documentation for implementing IDisposable has:
          >>
          >> Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
          >> If disposing Then
          >> ' Free other state (managed objects).
          >> End If
          >> Tag.Dispose()
          >> ' Free your own state (unmanaged objects).
          >> ' Set large fields to null.
          >> End Sub
          >>
          >> I am having trouble understanding the comments:
          >> What does "state (managed objects)" mean
          >> What does "state (unmanaged objects)" mean
          >> I understand managed objects and unmanaged objects like com objects,
          >> certain
          >> GDI objects are unmanaged and should be disposed here. But what does the
          >> "state" mean?
          >>
          >> --
          >> Dennis in Houston[/color][/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Idisposable

            Dennis,

            "Dennis" <Dennis@discuss ions.microsoft. com> schrieb:[color=blue]
            > a State Managed Object and a "your own State unmanaged object. For
            > example,
            > is a GDI Pen "state managed object" or my own state managed object"[/color]

            You should call a pen's 'Dispose' method explicitly when the class'
            'Dispose' method is called.

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

            Comment

            • Dennis

              #7
              Re: Idisposable

              Thanks...I understand a lot more now. I guess I can tell if a .net object is
              100% managed if it doesn't have a dispose method and vice versa, if it has a
              dispose method then it's not 100% managed.

              "Scott M." wrote:
              [color=blue]
              > Hi Dennis,
              >
              > I really don't know what a GDI Pen object is (I have no experience with it.
              > Having said that, it's really pretty simple...manage d objects are classes
              > that are instanced from and their code is run 100% by the Common Language
              > Runtime. Many of the classes in the .NET Framework are 100% managed
              > objects. Some however, are not. An OLEDBConnection is not 100% managed
              > because the underlying OLEDB Provider is COM based.
              >
              > An unmanaged object is a class that is NOT instanced by the CLR. So, any
              > non .NET objects (COM for example) would qualify here. Examples of
              > unmanaged objects would be any ActiveX control or other COM objects like
              > automating Office classes.
              >
              >
              > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
              > news:346D7D96-BBB2-4AD9-A523-5C46A48173E1@mi crosoft.com...[color=green]
              > > Scott, it would be very helpful if you could give me an example of :
              > >
              > > a State Managed Object and a "your own State unmanaged object. For
              > > example,
              > > is a GDI Pen "state managed object" or my own state managed object"
              > >
              > > I really appreciate you helping me to understand this.
              > >
              > > "Dennis" wrote:
              > >[color=darkred]
              > >> VB.Net Documentation for implementing IDisposable has:
              > >>
              > >> Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
              > >> If disposing Then
              > >> ' Free other state (managed objects).
              > >> End If
              > >> Tag.Dispose()
              > >> ' Free your own state (unmanaged objects).
              > >> ' Set large fields to null.
              > >> End Sub
              > >>
              > >> I am having trouble understanding the comments:
              > >> What does "state (managed objects)" mean
              > >> What does "state (unmanaged objects)" mean
              > >> I understand managed objects and unmanaged objects like com objects,
              > >> certain
              > >> GDI objects are unmanaged and should be disposed here. But what does the
              > >> "state" mean?
              > >>
              > >> --
              > >> Dennis in Houston[/color][/color]
              >
              >
              >[/color]

              Comment

              • Cor Ligthert

                #8
                Re: Idisposable

                Dennis,

                IDisposable defines a method to release allocated unmanaged resources.
                Unfortunality is this very inconsequently used (even on MSDN) and is as well
                spoken about unmanaged objects, what gives in my idea the big
                misunderstandin gs about this.

                In my opinion does dotNet itself not know unmanaged objects.

                Just my thought,

                Cor


                Comment

                • Dennis

                  #9
                  Re: Idisposable

                  I agree. You'd think that M'soft could write a system like .Net that knew
                  what an unmanaged object is. Better yet, why don't they finish .Net so
                  unmanaged objects aren't needed. I get tired of "disposing" of simple things
                  like Pens, Brushes, Data Base connections, etc. Every time I use one of the
                  ..Net objects, I have to check to see if it has a Dispose method.

                  "Cor Ligthert" wrote:
                  [color=blue]
                  > Dennis,
                  >
                  > IDisposable defines a method to release allocated unmanaged resources.
                  > Unfortunality is this very inconsequently used (even on MSDN) and is as well
                  > spoken about unmanaged objects, what gives in my idea the big
                  > misunderstandin gs about this.
                  >
                  > In my opinion does dotNet itself not know unmanaged objects.
                  >
                  > Just my thought,
                  >
                  > Cor
                  >
                  >
                  >[/color]

                  Comment

                  • Chad Z. Hower aka Kudzu

                    #10
                    Re: Idisposable

                    =?Utf-8?B?RGVubmlz?= <Dennis@discuss ions.microsoft. com> wrote in
                    news:892B61DF-D23E-4357-BD3B-11B3682CDF3F@mi crosoft.com:[color=blue]
                    > I agree. You'd think that M'soft could write a system like .Net that
                    > knew what an unmanaged object is. Better yet, why don't they finish
                    > .Net so unmanaged objects aren't needed. I get tired of "disposing" of
                    > simple things like Pens, Brushes, Data Base connections, etc. Every
                    > time I use one of the Net objects, I have to check to see if it has a
                    > Dispose method.[/color]

                    I agree that is a major PITA. For one - using should be able to be used
                    WHETHER or not it has a dispose...

                    But the underyling model of unmanaged vs managed is solid - and we will
                    always need the option for finalizatino and unmanaged objects. The problem
                    comes from a less than optimal implementation in the C# language (and even
                    worse in VB). Im not going to tout that Delphi is better - but how Borland
                    did Delphi.NET IMO is a lot more consistent and cleaner in many ways. In some
                    ways its less clean though because of compatibility with non .NET Delphi
                    though.


                    --
                    Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                    "Programmin g is an art form that fights back"

                    Make your ASP.NET applications run faster

                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Idisposable

                      Dennis,[color=blue]
                      > Better yet, why don't they finish .Net so
                      > unmanaged objects aren't needed.[/color]
                      IDisposable will still be needed/usable!

                      Consider limited 100% managed resources that are handed out my some form of
                      manager/repository class. IDisposable can be used to inform the object that
                      you are done with it, allowing the object to inform the manager/repostitory
                      its free now. Both the manager/repostitory & the class itself are 100%
                      managed...


                      VB.NET 2005 (aka Whidbey, due out later in 2005) in fact simplifies
                      IDisposable by adding the Using statement.



                      Using thePen As New Pen(...)
                      gr.DrawLine(the Pen, ...)
                      gr.DrawLine(the Pen, ...)
                      gr.DrawLine(the Pen, ...)
                      gr.DrawLine(the Pen, ...)
                      End Using

                      Then you only need to worry about using the pen, and not worry about
                      IDisposable or a Try/Finally.

                      Hope this helps
                      Jay


                      "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                      news:892B61DF-D23E-4357-BD3B-11B3682CDF3F@mi crosoft.com...[color=blue]
                      >I agree. You'd think that M'soft could write a system like .Net that knew
                      > what an unmanaged object is. Better yet, why don't they finish .Net so
                      > unmanaged objects aren't needed. I get tired of "disposing" of simple
                      > things
                      > like Pens, Brushes, Data Base connections, etc. Every time I use one of
                      > the
                      > .Net objects, I have to check to see if it has a Dispose method.
                      >
                      > "Cor Ligthert" wrote:
                      >[color=green]
                      >> Dennis,
                      >>
                      >> IDisposable defines a method to release allocated unmanaged resources.
                      >> Unfortunality is this very inconsequently used (even on MSDN) and is as
                      >> well
                      >> spoken about unmanaged objects, what gives in my idea the big
                      >> misunderstandin gs about this.
                      >>
                      >> In my opinion does dotNet itself not know unmanaged objects.
                      >>
                      >> Just my thought,
                      >>
                      >> Cor
                      >>
                      >>
                      >>[/color][/color]


                      Comment

                      Working...