2

Why the 2nd nested 'using' ( with my Foo, Bar classes) not generate the CA2202 "Do not dispose objects multiple times" warning. Same nested "using" for the IO types do generate the CA2202 issue.

Thank you all - any help will be appreciated.

using System;
using System.IO;

namespace ConsoleApplication1
{

    public class Foo : IDisposable
    {

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isUserCall)
        {
            if (isUserCall)
            {

            }
            else
            {

            }
        }
    }

    public class Bar : IDisposable
    {
        private Foo _foo;

        public Bar(Foo foo)
        {
            _foo = foo;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isUserCall)
        {
            if (isUserCall)
            {
                // call Foo::Dispose as StreamWriter dispose the FileStream
                _foo.Dispose();
            }
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            // generate FxCop CA2202 with .NET types
            using (Stream stream = new FileStream("file.txt", FileMode.OpenOrCreate))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    // Use the writer object...
                }
            }

            // not generate the issue with my types... why ?
            using (Foo f = new Foo())
            {
                using (Bar b = new Bar(f))
                {

                }
            }
        }
    }
}
liob
  • 21
  • 2
  • 1
    And, where is it disposing same object twice ? One using statement is disposing `Foo f` and another is `Bar b` – Parimal Raj Jan 01 '15 at 10:07
  • I think you should describe your problem before the pasted bit of code... would make your Q more readable. – Kris Jan 01 '15 at 10:12
  • Hi, inside the implementation of Bar::Dispose( bool )... – liob Jan 01 '15 at 10:13
  • Where's your program? – RenniePet Jan 01 '15 at 10:21
  • 1
    Your usage of Foo and Bar are 100% correct - that's the way things should be, you instantiate an object, Foo or Bar, and later you dispose of it. Doesn't matter if one is "nested" inside the other. The case involving FileStream and StreamWriter is actually due to a problematic design decision by Microsoft: they say that disposing of StreamWriter ALSO disposes the stream object it is based on. In my opinion this was actually a design mistake. More about that here: http://stackoverflow.com/questions/12000136/using-statement-filestream-and-or-streamreader-visual-studio-2012-warnings – RenniePet Jan 01 '15 at 14:32
  • Hi, thanks for the comment but the "foo bar" code also dispose the foo instance twise. Once when the bar instance dispose when complete the inner using.. Then when the outer using dispose the foo instance for the 2nd time.. Am I missing somthing? – liob Jan 02 '15 at 23:42
  • I share your wonder. It seems the analytics rule has a list of known problematic classes in the framework. – Rolf Sep 22 '15 at 13:56

0 Answers0