Home Dashboard Directory Help
Search

Disposable objects and CA2000, CA2202 warnings by OlegKap


Status: 

Closed
 as Duplicate Help for as Duplicate


6
0
Sign in
to vote
Type: Bug
ID: 611525
Opened: 10/7/2010 11:48:39 AM
Access Restriction: Public
0
Workaround(s)
view
2
User(s) can reproduce this bug

Description

I submitted the bug #591989 and was offered a solution which dont seems to work.
No matter what I do I am getting either CA2000 or CA2202 warning.

I am following MSFT examples found online and in proposed solution but no luck.

See steps to reproduce for more infor.

I followed step number 2 as you suggested but it still gave me a warning.
I did some research and it seems that BinaryReader and NetworkReader are disposing the stream that I passed it to the constructor. It seems odd to me to dispose of the object that you didnt create.


Please advise,

Thanks,
Oleg
Details
Sign in to post a comment.
Posted by 2re on 10/20/2011 at 12:24 AM
Where is the link for the duplicate so that one can vote on it?
Posted by OlegKap on 10/14/2010 at 7:28 AM
I tried the solution you proposed but I still getting the same warning.

Here the program that I am running:

using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new byte[] { 1, 2, 3, 4, 5 };
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream(data);
                using (var br = new BinaryReader(ms))
                {
                }
                ms = null;
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
            }
        }
    }
}

And I am getting this warning:
Warning    1    CA2202 : Microsoft.Usage : Object 'ms' can be disposed more than once in method 'Program.Main(string[])'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 23    H:\Projects\Test\ConsoleApplication1\ConsoleApplication1\Program.cs    23    ConsoleApplication1


This error ehappens if I run it using .NET Framework 4 or .NET Framework 4 Client Profile.

Few comments:
I doesnt make sence to assign anything to null since it should be cleaned up as soon as varaible goes out of scope, which is immediatly.
Also, it looks like the problem cause by the fact that BinaryReader disposes object it didnt create.
Should I submit it as a different problem?
Posted by Microsoft on 10/13/2010 at 1:39 PM
Hi,

The solution mentioned in the doc referred in 591989 does work. The key statement that's missing in your repro is the line that sets ms back to null. The following code should work without CA2202:

static void Main(string[] args)
{
var data = new byte[] { 1, 2, 3, 4, 5 };
MemoryStream ms = null;
try
{
    ms = new MemoryStream(data);
    using (var br = new BinaryReader(ms))
    {
     ms = null; // <-- this line sets it back to null to indicate that br will dispose ms and it doesn't need to be disposed again in the finally block
    }
}
finally
{
    if (ms != null)
    {
     ms.Dispose();
    }
}
}

thanks,
Visual Studio Code Analysis team
Posted by Microsoft on 10/7/2010 at 7:04 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 10/7/2010 at 12:23 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.