1

编辑:

这个问题不是重复的!,也许是一个常见的问题“这个警告是什么意思? ”但是当我问如何在特定代码中修复警告时不是重复的,而且我自己也不知道警告的意义。–


VS 中的代码分析向我显示了这个警告:

CA2202 不要多次处理对象 对象“OutputStream”可以在方法“FileSplitter.Split(String, Long, String, String, Boolean, Boolean)”中多次处理。为避免生成 System.ObjectDisposedException,您不应在对象上多次调用 Dispose。:行:490 WindowsApplication1 FileSplitter.vb 490

这条线490是这样的:

End Using ' OutputStream

它给了我对 line 中的另一个对象同样的警告498,这个:

End Using ' InputStream

但我认为我正确使用了Using关键字并且我不会多次处理对象,如果我做错了什么,那么我该如何修复我的代码?

这是完整的块:

    ' Open the file to start reading bytes.
    Using InputStream As New FileStream(fInfo.FullName, FileMode.Open)

        Using BinaryReader As New BinaryReader(InputStream)

            While (InputStream.Position < InputStream.Length)

                ' Create the chunk file to Write the bytes.
                Using OutputStream As New FileStream(ChunkFile, FileMode.Create)

                    Using BinaryWriter As New BinaryWriter(OutputStream)

                        ' Read until reached the end-bytes of the input file.
                        While (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
                            ' Some irrelevant code here...

                        End While ' (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)

                        OutputStream.Flush()

                    End Using ' BinaryWriter

                End Using ' OutputStream

            End While ' InputStream.Position < InputStream.Length

        End Using ' BinaryReader

    End Using ' InputStream

更新

添加 try/catch 块后,它仍然显示相同的警告。如果我删除最后一次尝试/捕获,它只会显示 1 个警告。

    ' Open the file to start reading bytes.
    Dim InputStream As Stream = Nothing

    Try

        InputStream = New FileStream(fInfo.FullName, FileMode.Open)
        Using BinaryReader As New BinaryReader(InputStream)

            While (InputStream.Position < InputStream.Length)

                ' Create the chunk file to Write the bytes.
                Dim OutputStream As Stream = Nothing

                Try
                    OutputStream = New FileStream(ChunkFile, FileMode.Create)
                    Using BinaryWriter As New BinaryWriter(OutputStream)

                        ' Read until reached the end-bytes of the input file.
                        While (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)

                    End Using ' BinaryWriter

                Catch ex As Exception
                    Throw New Exception(ex.Message)

                Finally
                    If OutputStream IsNot Nothing Then
                        OutputStream.Dispose()
                    End If

                End Try

            End While ' InputStream.Position < InputStream.Length

        End Using ' BinaryReader

    Catch ex As Exception
        Throw New Exception(ex.Message)

    Finally
        If InputStream IsNot Nothing Then
            InputStream.Dispose()
        End If

    End Try
4

1 回答 1

2

这个问题的原因在您的代码中并不明显,而只是代码分析对所涉及的对象的了解。

BinaryReader具体来说,当您构造or的一个实例时BinaryWriter,您将底层流的所有权授予这些对象。因此,当您处理读取器/写入器对象时,流也会被处理。

因此,当您在处理完读取器/写入器对象后继续处理底层流时,代码分析会对此发出警告。

现在,这会是个问题吗?不。

你应该修复它吗?一般来说,如果您启用代码分析,您应该修复所有错误或警告,除非您有充分的理由不这样做。

为了“正确”摆脱这个问题,习惯上将外部流包装在一个 try/finally 块中,并且只有在没有构造读取器/写入器的情况下,您的代码才能以某种方式到达 finally 块。

换句话说,如果您实际上并未将对象的所有权授予读取器/写入器对象,则您只想处置底层流。

我认为特定规则还提供了如何执行此操作的示例。

于 2014-08-15T17:13:31.293 回答