Re: Disposing Collections

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance





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.jensen@xxxxxxx sip:helge.jensen@xxxxxxx -=> Sebastian cover-music: http://ungdomshus.nu <=- .



Relevant Pages

  • Re: Performance of try {} finally {} blocks
    ... Try running a profiler on your code, you will soon discover that the try/finally clauses are not taking up any part of execution time worth mentioning. ... void function() { ... If the cleanup-code taking up too much space you can consider whether using Dispose is actually important or if it can be safely disposed of. ... protected void performActionX ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Using Objects from another class to decrease Mem Usage
    ... and then call Dispose *immediately* after you ... IDisposable has no effect on the memory usage if your resources are ... static void Main ... if the GC runs after DoSomething it will not touch the test ...
    (microsoft.public.dotnet.general)
  • Re: Issue: Queued Component and Marshal.ReleaseComObject
    ... Originally I have my interface inherit the IDisposable and I got ... up the ideal to explicitly define the Dispose() method in the interface, ... > void DisplayMessage; ... > public void DisplayMessage(string msg) ...
    (microsoft.public.dotnet.framework.interop)
  • Re: EXCEL.EXE remains in processes after Dispose...
    ... void DoSomethingWithExcel() ... This way, you don't even need to bother about calling dispose, etc. ... I created an ArrayList to store every Excel object that is created and use ... Here is the new code in the Dispose method of my Excel ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: what are unmanaged resources
    ... > the GC will come along and clean up all the memory and resources associated ... > in a deterministic fashion and clean up unmanaged resources. ... > placed inside the Dispose functions. ... > the IDisposable interface i.e. it has a "public void Dispose()" method then ...
    (microsoft.public.dotnet.languages.csharp)