Home All Groups Group Topic Archive Search About
Author
25 May 2005 2:03 PM
gmccallum
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

Author
25 May 2005 2:18 PM
Carlos J. Quintero [.NET MVP]
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

Show quoteHide quote
"gmccallum" <gmccal***@discussions.microsoft.com> escribió en el mensaje
news:0C45F912-9200-4554-912C-FCA7C6CC4F5A@microsoft.com...
> 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
>
Are all your drivers up to date? click for free checkup

Author
25 May 2005 2:56 PM
gmccallum
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(class1)) class1.dispose()

Show quoteHide quote
"Carlos J. Quintero [.NET MVP]" wrote:

> 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" <gmccal***@discussions.microsoft.com> escribió en el mensaje
> news:0C45F912-9200-4554-912C-FCA7C6CC4F5A@microsoft.com...
> > 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
> >
>
>
>
Author
25 May 2005 3:51 PM
Carlos J. Quintero [.NET MVP]
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:
http://www.mztools.com


Show quoteHide quote
"gmccallum" <gmccal***@discussions.microsoft.com> escribió en el mensaje
news:1A1983B9-EF21-4676-A36C-1F9D50AA809A@microsoft.com...
> 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(class1)) class1.dispose()
Author
25 May 2005 5:42 PM
Helge Jensen
gmccallum wrote:
> 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(class1)) class1.dispose()

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.SuppressFinalize(this);
     if ( s != null ) {
       s.Dispose();
       s = null;
     }
   }
   ~Bar() {
    // This code should not run in well-written programs
    Errors.ForgottenDispose(this);
    Dispose();
   }
}


--
Helge Jensen
   mailto:helge.jen***@slog.dk
   sip:helge.jen***@slog.dk
                -=> Sebastian cover-music: http://ungdomshus.nu <=-



Post Thread options