Disposing Collections

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

    #1

    Disposing Collections

    This whole dispose topic confuses me regarding managed and unmanaged
    resources. I think that it was easier when you just cleaned up every object,
    which brings me to my problem.

    I have some classes that have Hashtables collections defined as class
    variables.
    In the past, when I was done with the class, I would remove all the items
    from the collection and then set the collection to null.

    In C# (.NET General), is this necessary anymore? For example, do I need to
    implement a dispose method that would remove all the items from the
    collection and set the collection to null. Is it enough to just set the
    class instance to null and everything inside would be cleaned up?

    Thanks in advance.
    Greg

  • Carlos J. Quintero [.NET MVP]

    #2
    Re: Disposing Collections

    No, you don´t need to implement the dispose method in that case.

    - If a class has a Dispose method, you need to call it when you are done
    with it. For example, if the objects that you store in the collection have
    the Dispose method and the collection is the only reference to them, you
    should dispose them before removing them from the collection.

    - You only need to implement a Dispose method in your class when it wraps an
    unmaneged resource, such as a Win32 handle, or a managed object that has a
    Dispose method that your class don´t call automatically internally.

    --

    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
    You can code, design and document much faster.
    Free resources for add-in developers:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


    "gmccallum" <gmccallum@disc ussions.microso ft.com> escribió en el mensaje
    news:0C45F912-9200-4554-912C-FCA7C6CC4F5A@mi crosoft.com...[color=blue]
    > This whole dispose topic confuses me regarding managed and unmanaged
    > resources. I think that it was easier when you just cleaned up every
    > object,
    > which brings me to my problem.
    >
    > I have some classes that have Hashtables collections defined as class
    > variables.
    > In the past, when I was done with the class, I would remove all the items
    > from the collection and then set the collection to null.
    >
    > In C# (.NET General), is this necessary anymore? For example, do I need
    > to
    > implement a dispose method that would remove all the items from the
    > collection and set the collection to null. Is it enough to just set the
    > class instance to null and everything inside would be cleaned up?
    >
    > Thanks in advance.
    > Greg
    >[/color]


    Comment

    • gmccallum

      #3
      Re: Disposing Collections

      Thank you. Is there a way to tell in code if an object implements the
      Dispose method without having to try...catch exceptions.
      For example
      [not meant to be c# code]
      if (isdisposable(c lass1)) class1.dispose( )

      "Carlos J. Quintero [.NET MVP]" wrote:
      [color=blue]
      > No, you don´t need to implement the dispose method in that case.
      >
      > - If a class has a Dispose method, you need to call it when you are done
      > with it. For example, if the objects that you store in the collection have
      > the Dispose method and the collection is the only reference to them, you
      > should dispose them before removing them from the collection.
      >
      > - You only need to implement a Dispose method in your class when it wraps an
      > unmaneged resource, such as a Win32 handle, or a managed object that has a
      > Dispose method that your class don´t call automatically internally.
      >
      > --
      >
      > Best regards,
      >
      > Carlos J. Quintero
      >
      > MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
      > You can code, design and document much faster.
      > Free resources for add-in developers:
      > http://www.mztools.com
      >
      > "gmccallum" <gmccallum@disc ussions.microso ft.com> escribió en el mensaje
      > news:0C45F912-9200-4554-912C-FCA7C6CC4F5A@mi crosoft.com...[color=green]
      > > This whole dispose topic confuses me regarding managed and unmanaged
      > > resources. I think that it was easier when you just cleaned up every
      > > object,
      > > which brings me to my problem.
      > >
      > > I have some classes that have Hashtables collections defined as class
      > > variables.
      > > In the past, when I was done with the class, I would remove all the items
      > > from the collection and then set the collection to null.
      > >
      > > In C# (.NET General), is this necessary anymore? For example, do I need
      > > to
      > > implement a dispose method that would remove all the items from the
      > > collection and set the collection to null. Is it enough to just set the
      > > class instance to null and everything inside would be cleaned up?
      > >
      > > Thanks in advance.
      > > Greg
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Carlos J. Quintero [.NET MVP]

        #4
        Re: Disposing Collections

        Since disposable objects should implement the IDisposable interface, you can
        use: if (obj1 is IDisposable)

        --

        Best regards,

        Carlos J. Quintero

        MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
        You can code, design and document much faster.
        Free resources for add-in developers:
        MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



        "gmccallum" <gmccallum@disc ussions.microso ft.com> escribió en el mensaje
        news:1A1983B9-EF21-4676-A36C-1F9D50AA809A@mi crosoft.com...[color=blue]
        > Thank you. Is there a way to tell in code if an object implements the
        > Dispose method without having to try...catch exceptions.
        > For example
        > [not meant to be c# code]
        > if (isdisposable(c lass1)) class1.dispose( )[/color]


        Comment

        • Helge Jensen

          #5
          Re: Disposing Collections



          gmccallum wrote:[color=blue]
          > Thank you. Is there a way to tell in code if an object implements the
          > Dispose method without having to try...catch exceptions.[/color]
          [color=blue]
          > For example
          > [not meant to be c# code]
          > if (isdisposable(c lass1)) class1.dispose( )[/color]

          IDisposable objects (usually) represents resouces which should be
          released at-once when you are done using them. This introduces a concept
          of "ownership" of the disposable.

          You shouldn't Dispose() just because you are passes an IDisposable. only
          the "owner" should do that.

          Usually ownership is on the callstack, which fits the "using" idiom very
          nicely, Example:

          class Foo {
          void WriteTo(Stream s) {
          s.Write(...);
          // just use stream, don't dispose
          }
          void f() {
          using ( Stream s = new File.Open("foo" ) ) // "own" s
          WriteTo(s);
          }
          }

          Another case is where objects have "owned" members, they often become
          resources themselves, becoming candidates for disposal:

          class Bar: IDisposable {
          Stream s;
          public Bar() { s = new File.Open("foo" ); }
          public void Dispose() {
          GC.SuppressFina lize(this);
          if ( s != null ) {
          s.Dispose();
          s = null;
          }
          }
          ~Bar() {
          // This code should not run in well-written programs
          Errors.Forgotte nDispose(this);
          Dispose();
          }
          }


          --
          Helge Jensen
          mailto:helge.je nsen@slog.dk
          sip:helge.jense n@slog.dk
          -=> Sebastian cover-music: http://ungdomshus.nu <=-

          Comment

          Working...