警告:不要多次丢弃对象

时间:2014-08-15 17:06:31

标签: .net vb.net dispose using objectdisposedexception

修改

这个问题不重复!可能是一个常见的问题“ 这意味着什么? ”但是当我问如何在特定的代码中修复警告,我甚至不知道警告的重要性。 -


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

  

CA2202不要多次处置对象'对象'OutputStream'可以   方法'FileSplitter.Split中不止一次处理(String,Long,   String,String,Boolean,Boolean)'。为了避免产生   System.ObjectDisposedException你不应该调用Dispose   一次在物体上:线条:   490 WindowsApplication1 FileSplitter.vb 490

490行是:

End Using ' OutputStream

它为行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

1 个答案:

答案 0 :(得分:2)

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

具体来说,当您构造BinaryReaderBinaryWriter的实例时,您将基础流的所有权交给这些对象。因此,当您处理读取器/写入器对象时,也会处理流。

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

现在,这会有问题吗?否。

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

为了“正确地”解决这个问题,通常将外部流包装在try / finally块中,并且只有在不构造读取器/写入器的情况下,您的代码才会以某种方式到达finally块。 / p>

换句话说,如果您实际上没有将对象的所有权赋予读取器/编写器对象,则只需要处置基础流。

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

相关问题