0

I am in a way to refactor,where I came across this weird code. It seems like a self contradicting code to me. Am I right? I just want to know potential issues with this. Kindly note same code has been used at many places where there are File Operations, Network operations and Un-managed resources usages and needs a proper cleanup.

class xyz
{

      //my class members having n/w , file operations
      public void SomeFileIOActivity()
      {
         //File R/W operation codes
      }



      //Dispose method
      public void Dispose()
      {
        if (!_disposed)
        {
            _disposed = true;
        }
        GC.SuppressFinalize(this);
      }

      //destructor
      ~FileComparer()
      {
        Dispose();
      }
}

Its usage in another class

var o = new xyz();
o.SomeFileIOActivity();
  • Might be MemoryLeak – Mohit S Dec 02 '16 at 04:36
  • Is this from something that implements IDisposable? If so this code is to stop the garbage collector from disposing of this item before the IDisposable framework is finished with it. – meganaut Dec 02 '16 at 04:37
  • A wonderful explanation has been found on [Why call Dispose()?](http://stackoverflow.com/a/15915658/3796048) – Mohit S Dec 02 '16 at 04:37
  • 1
    There's no reason to implement `Dispose` if it does nothing. If you indeed have references that need to be cleaned up, `GC.SuppressFinalize` is going to cause problems. – Rob Dec 02 '16 at 04:38
  • Looks like half-cleaned-up removal of correct IDisposable implementation. Most likely due to misuse of `using` for long lived stream classes... Exact code as shown is safe to remove as @rob pointed out, if actual code has more complete IDisposable implementation (i.e. `virtual` dispose) than you should be more careful. – Alexei Levenkov Dec 02 '16 at 05:09

0 Answers0