ArrayLists and Dispose

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

    #1

    ArrayLists and Dispose

    I have an arraylist of class objects and the class contains unmanaged
    resources. When I remove an item from the arraylist using the .remove or
    ..removeat method, will the object's dispose method be called automatically or
    do I have to call it before removing it from the ArrayList?
    --
    Dennis in Houston
  • Tom Shelton

    #2
    Re: ArrayLists and Dispose

    On 2005-07-27, Dennis <Dennis@discuss ions.microsoft. com> wrote:[color=blue]
    > I have an arraylist of class objects and the class contains unmanaged
    > resources. When I remove an item from the arraylist using the .remove or
    > .removeat method, will the object's dispose method be called automatically or
    > do I have to call it before removing it from the ArrayList?[/color]

    Nope... In this case, you might want your collection to be inherit from
    System.Collecti ons.CollectionB ase. Then, in your subclass, override
    it's OnRemoveComplet e method.

    --
    Tom Shelton [MVP]

    Comment

    • Ben

      #3
      Re: ArrayLists and Dispose

      Hi, Tom. Is there a way to put a break point on dispose class (wherever that
      is on the dotNET framework) to see whenever VB is calling it or not?

      "Tom Shelton" wrote:
      [color=blue]
      > On 2005-07-27, Dennis <Dennis@discuss ions.microsoft. com> wrote:[color=green]
      > > I have an arraylist of class objects and the class contains unmanaged
      > > resources. When I remove an item from the arraylist using the .remove or
      > > .removeat method, will the object's dispose method be called automatically or
      > > do I have to call it before removing it from the ArrayList?[/color]
      >
      > Nope... In this case, you might want your collection to be inherit from
      > System.Collecti ons.CollectionB ase. Then, in your subclass, override
      > it's OnRemoveComplet e method.
      >
      > --
      > Tom Shelton [MVP]
      >[/color]

      Comment

      • Tom Shelton

        #4
        Re: ArrayLists and Dispose

        On 2005-07-27, Ben <Ben@discussion s.microsoft.com > wrote:[color=blue]
        > Hi, Tom. Is there a way to put a break point on dispose class (wherever that
        > is on the dotNET framework) to see whenever VB is calling it or not?[/color]

        Well that depends... Dispose is a method of the IDisposable interface,
        so if it's your class and you have implemented dispose, sure you can put
        a break in it. If it isn't then that is a little more difficult :)

        But, removing an object won't call dispose. You won't have the
        reference any more, so it will get it's Finalize method called at the
        next GC - but that's probably not what you want. Like I said, the
        easiest way to do what you want is to create a class that inherits from
        CollectionBase and then override it's OnRemoveComplet e method.

        --
        Tom Shelton [MVP]

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: ArrayLists and Dispose

          Dennis,

          20% of the classes of dotnet implements directly IDisposable because of the
          fact that they inherit from componentmodel, those are probably 80% of the
          classes you normally use.

          In those you don't normally have to worry about Dispose. (That is as well
          the reason that those classes have a dispose method. The same as all classes
          have a ToString and GetType method because all classes inherits from Object)
          It is up to you if you use that GetType in your program.

          http://msdn.microsoft.com/library/de...mberstopic.asp

          I hope this helps,

          Cor


          Comment

          • Dennis

            #6
            Re: ArrayLists and Dispose

            Thanks for the answer. If I understand it correctly, I can just let Finalize
            and GC to the work or implement my own arraylist class using the
            CollectionBase Class as my base class if I have unmanaged resources to
            dispose of.

            --
            Dennis in Houston


            "Cor Ligthert [MVP]" wrote:
            [color=blue]
            > Dennis,
            >
            > 20% of the classes of dotnet implements directly IDisposable because of the
            > fact that they inherit from componentmodel, those are probably 80% of the
            > classes you normally use.
            >
            > In those you don't normally have to worry about Dispose. (That is as well
            > the reason that those classes have a dispose method. The same as all classes
            > have a ToString and GetType method because all classes inherits from Object)
            > It is up to you if you use that GetType in your program.
            >
            > http://msdn.microsoft.com/library/de...mberstopic.asp
            >
            > I hope this helps,
            >
            > Cor
            >
            >
            >[/color]

            Comment

            • Tom Shelton

              #7
              Re: ArrayLists and Dispose

              On 2005-07-27, Dennis <Dennis@discuss ions.microsoft. com> wrote:[color=blue]
              > Thanks for the answer. If I understand it correctly, I can just let Finalize
              > and GC to the work or implement my own arraylist class using the
              > CollectionBase Class as my base class if I have unmanaged resources to
              > dispose of.
              >[/color]

              Pretty much...

              --
              Tom Shelton [MVP]

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: ArrayLists and Dispose

                Dennis,

                As addition, you can open a component instead of a class or a form.
                You get than a complete template.

                I hope this helps,

                Cor


                Comment

                Working...