# V3178. Calling method or accessing property of potentially disposed object may result in exception.

The analyzer detected the method call or accessing the property of an object for which the 'Dispose' method or its equivalent was called earlier. It may result in the exception thrown.

Let's look at the example:

    public void AppendFileInformation(string path)
    {
      FileStream stream = new FileStream(path,
                                         FileMode.Open,
                                         FileAccess.Read);
      ....
      stream.Close();
      ....
      if (stream.Length == stream.Position)
      {
        Console.WriteLine("End of file has been reached.");
        ....
      }
      ....
    }

The condition checks if the entire file has been read. The check compares the current position of the stream and the stream length. But accessing the 'Length' property results in 'ObjectDisposedException'. The reason for exception is that the 'Close' method is called for the 'stream' variable above its condition. For the 'FileStream' class, the 'Close' method is equivalent to the 'Dispose' method. Therefore, the 'stream' resources are released.

Let's examine the correct implementation of 'AppendFileInformation':

    public void AppendFileInformation(string path)
    {
      using (FileStream stream = new FileStream(path,
                                                FileMode.Open,
                                                FileAccess.Read))
      {
        ....
        if (stream.Length == stream.Position)
        {
          Console.WriteLine("End of file has been reached.");
        }
        ....
      }
      ....
    }

For the method to operate correctly, the 'using' statement is better. In this case:

- at the end of the scope defined by the 'using' statement, the 'stream' resources are released automatically;
- outside the boundaries of the 'using' statement, the object cannot be accessed. It gives extra protection against exceptions of the 'ObjectDisposedException' type;
- even if an exception occurs in the 'using' statement, the resources are still released.

One more error may be as following:

    public void ProcessFileStream(FileStream stream)
    {
      ....
      bool flag = CheckAndCloseStream(stream);
      AppendFileInformation(stream);
      ....
    }

    public bool CheckAndCloseStream(FileStream potentiallyInvalidStream)
    {
      ....
      potentiallyInvalidStream.Close();
      ....
    }

    public void AppendFileInformation(FileStream streamForInformation)
    {
      ....
      if (streamForInformation.Length == streamForInformation.Position)
      {
        Console.WriteLine("End of file has been reached.");
      }
      ....
    }

The resources of an object (referenced by the 'stream' variable) are released after we call 'CheckAndCloseStream' in the 'ProcessFileStream' method. The 'stream' variable is then passed to the 'AppendFileInformation' method. Accessing the 'Length' property inside the method results in 'ObjectDisposedException'.

The correct implementation of 'ProcessFileStream' would be as following:

    public void ProcessFileStream(FileStream stream)
    {
      ....
      AppendFileInformation(stream);
      bool flag = CheckAndCloseStream(stream);
      ....
    }

The 'CheckAndCloseStream' method call swapped places with the 'AppendFileInformation' method call. As a result, the 'stream' resources are released after other actions are performed. Therefore, no exception is thrown.

This diagnostic is classified as:

CWE-672