# V3073. Not all IDisposable members are properly disposed. Call 'Dispose' when disposing 'A' class.

The analyzer detected a possible error in a class implementing the 'IDisposable' interface. The 'Dispose' method is not called in the 'Dispose' method of the class on some of the fields whose type implements the 'IDisposable' interface. It is very likely that the programmer forgot to free some resources after use.

Consider the following example:

    class Logger : IDisposable
    {
      FileStream fs;
      public Logger() {
        fs = File.OpenWrite("....");
      }
      public void Dispose() { }
    }

This code uses a wrapper class, 'Logger', implementing the 'IDisposable' interface, which allows writing to a log file. This class, in its turn, contains variable 'fs', which is used to perform the writing. Since the programmer forgot to call method 'Dispose' or 'Close' in the 'Dispose' method of the 'Logger' class, the following error may occur.

Suppose an object of the 'Logger' class was created in the 'using' block:

    using(Logger logger = new Logger()){
      ....
    }

As a result, method 'Dispose' will be called on the 'logger' object before leaving the 'using' block.

Such use implies that all the resources used by the object of class 'Logger' have been freed and you can use them again.

In our case, however, the 'fs' stream, writing to a file, won't be closed; and when trying to access this file again from another stream, for example, an access error may occur.

It is a heisenbug because the 'fs' object will free the opened file as this object is being cleared by the garbage collector. However, clearing of this object is a non-deterministic event; it's not guaranteed to take place after the 'logger' object leaves the 'using' block. A file access error occurs if the file is opened before the garbage collector has cleared the 'fs' object.

To solve this issue, we just need to call 'fs.Dispose()' in the 'Dispose' method of the 'Logger' class:

    class Logger : IDisposable
    {
      FileStream fs;
      public Logger() {
        fs = File.OpenWrite("....");
      }
      public void Dispose() {
        fs.Dispose();
      }
    }

This solution guarantees that the file opened by the 'fs' object will be freed by the moment of leaving the 'using' block.