What is causing 'CA2202: Do not dispose object

2019-01-15 15:58发布

I have the function below which is used to serialize an object without adding the XML declaration. I've just opened the project containing it an Visual Studio 2012 and the Code Analysis is coming up with the 'CA2202: Do not dispose objects multiple times' warning.

Now in other cases I've fixed this warning by removing an [object].Close that wasn't needed but in this case I can't see what needs to be altered and the help for the warning while being accurate isn't exactly informative as to how it is caused or how to fix it.

What exactly is causing the warning to display and how can I refactor to avoid it?

''' <summary>
''' Serialize an object without adding the XML declaration, etc.
''' </summary>
''' <param name="target"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function SerializeElementToText(Of T As New)(target As T) As String
    Dim serializer As New XmlSerializer(GetType(T))
    'Need to serialize without namespaces to keep it clean and tidy
    Dim emptyNS As New XmlSerializerNamespaces({XmlQualifiedName.Empty})
    'Need to remove xml declaration as we will use this as part of a larger xml file
    Dim settings As New XmlWriterSettings()
    settings.OmitXmlDeclaration = True
    settings.NewLineHandling = NewLineHandling.Entitize
    settings.Indent = True
    settings.IndentChars = (ControlChars.Tab)
    Using stream As New StringWriter(), writer As XmlWriter = XmlWriter.Create(stream, settings)
        'Serialize the item to the stream using the namespace supplied
        serializer.Serialize(writer, target, emptyNS)
        'Read the stream and return it as a string
        Return stream.ToString
    End Using 'Warning jumps to this line
End Function

I tried this but it doesn't work either:

    Using stream As New StringWriter()
        Using writer As XmlWriter = XmlWriter.Create(stream, settings)
            serializer.Serialize(writer, target, emptyNS)
            Return stream.ToString
        End Using
    End Using 'Warning jumps to this line instead

2条回答
ら.Afraid
2楼-- · 2019-01-15 16:11

Try to modify the code to have 2 separate usings:

Using stream As New StringWriter()
    Using writer As XmlWriter = XmlWriter.Create(stream, settings)

    End Using
End Using
查看更多
可以哭但决不认输i
3楼-- · 2019-01-15 16:16

It is a false warning, caused by XmlWriter disposing the stream you pass. Which makes your StringWriter disposed twice, first by XmlWriter and again by your Using statement.

This is not a problem, disposing .NET framework objects twice is not an error and doesn't cause any trouble. It could be a problem if the Dispose() method is poorly implemented, FxCop doesn't take its chances to not tell you about it because it isn't otherwise smart enough to know if a Dispose() method is correct.

There is not any way to rewrite the code to avoid a warning. StringWriter doesn't actually have anything to dispose so moving it out of the Using statement is okay. But that will produce another warning, CA2000. Best thing to do is to just ignore this warning. Use the SuppressMessageAttribute if you don't want to look at it again.

查看更多
登录 后发表回答