Search

CA2000 and CA2202 Offer contradictory warnings by Jonathan Allen

Closed
as Postponed Help for as Postponed

12
0
Sign in
to vote
Type: Bug
ID: 535118
Opened: 2/21/2010 12:41:13 AM
Access Restriction: Public
0
Workaround(s)
9
User(s) can reproduce this bug
Correcting CA2000 will result in warning CA2202. Correcting CA2202 returns us back to CA2000.
Details (expand)

Product Language

English

Version

Visual Studio 2010 Release Candidate

Operating System

Windows 7

Operating System Language

English

Steps to Reproduce

Version 1

    Private Sub CaptureCurrentLog()
        Using source As New StreamReader(New FileStream(m_CurrentFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            File.WriteAllText(m_CaptureFile, source.ReadToEnd)
        End Using
    End Sub


Version 2

    Private Sub CaptureCurrentLog()
        Using fileSource As FileStream = New FileStream(m_CurrentFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Using source As New StreamReader(fileSource)
                File.WriteAllText(m_CaptureFile, source.ReadToEnd)
            End Using
        End Using
    End Sub

Actual Results

Version 1

Warning    3    CA2000 : Microsoft.Reliability : In method 'BaileyLogger.CaptureCurrentLog()', object 'New FileStream(Me.m_CurrentFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'New FileStream(Me.m_CurrentFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)' before all references to it are out of scope.    C:\Users\Grauenwolf\Documents\Dashboards\Aam.Dashboards.Core\BaileyLogger.vb    252    Aam.Dashboards.Core


Version 2

Warning    3    CA2202 : Microsoft.Usage : Object 'fileSource' can be disposed more than once in method 'BaileyLogger.CaptureCurrentLog()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 256    C:\Users\Grauenwolf\Documents\Dashboards\Aam.Dashboards.Core\BaileyLogger.vb    256    Aam.Dashboards.Core

Expected Results

No warnings or only correctable warnings.
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 8/5/2010 at 1:23 PM
We've added this issue to our feature backlog for consideration in a future release of Visual Studio.
Posted by Thomas_Eyde on 5/5/2010 at 11:11 AM
Can someone at Microsoft please provide a working piece of code, as I have yet to see something that actually works.

Take the following code in C#:

                using (var taxFile = File.Open(@"c:\thisfiledoesntExist.foo", FileMode.Open))
                {
                    var reader = new StreamReader(taxFile);
                    return int.Parse(reader.ReadLine() ?? "");
                }

the taxFile object should be disposed just fine. Who cares if the reader object tries to do the same when the garbage collector arrives to the party? Everything is out of scope, so nothing bad can happen.
Posted by Microsoft on 2/23/2010 at 11:35 AM
Hi,

Thank you for your feedback. In version 1 of your repro, CA2000 gives the warning because if StreamReader's constructor throws an exception, the newly created FileStream object is not disposed. In version 2, CA2202 gives the warning because new StreamReader object will assume ownership of the FileStream object and will dispose it. At the end of the using block around the FileStream object, it will dispose the FileStream object a second time. To fix this, you can change the outer using block, which is the one around the FileStream object, and have it dispose the FileStream object depending on whether the StreamReader object was created successfully.
We appreciate your valuable feedback and we will consider incorporating it in a future release of Visual Studio.

thanks,
Brahmnes
Posted by Microsoft on 2/23/2010 at 1:22 AM

Thanks for your feedback.

We are routing 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 2/22/2010 at 7:04 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.