[Mono-bugs] [Bug 539345] New: using-statement fails to call Dispose(), instead allocates new instance and disposes that

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Sep 15 13:04:55 EDT 2009


           Summary: using-statement fails to call Dispose(), instead
                    allocates new instance and disposes that
    Classification: Mono
           Product: Mono: Compilers
           Version: 2.4.x
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: oskar.berggren at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

User-Agent:       Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:
Gecko/2009090217 Ubuntu/9.04 (jaunty) Firefox/3.0.14

In the following code, there are exactly two occurrences of "new Foo()". Yet
three instances are created, of which only two are disposed! If I'm not missing
anything, this seems like a rather serious bug, only mitigated by the fact that
it is probably quite rare to have using(IDisposable a = ...). Such code does
occur when using Rhino.Mocks for unit tests.

Code and output below:

using System;

namespace IDisposableTest
    class MainClass
        public static void Main(string[] args)
            using (Foo f = new Foo())

            Console.WriteLine("Between. Foo.TotalInstances = " +

            using (IDisposable f = new Foo())

            Console.WriteLine("After. Foo.TotalInstances = " +

    class Foo : IDisposable
        public static int TotalInstances = 0;

        private int my_a = 0;

        public Foo()
            my_a = TotalInstances++;
            Console.WriteLine("Instance " + my_a + " ctor");

        public void Dispose()
            Console.WriteLine("Instance " + my_a + " Dispose()");


Instance 0 ctor
Instance 0 Dispose()
Between. Foo.TotalInstances = 1
Instance 1 ctor
Instance 2 ctor
Instance 2 Dispose()
After. Foo.TotalInstances = 3

Reproducible: Always

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.

More information about the mono-bugs mailing list