Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 警告:不要多次处置对象_.net_Vb.net_Dispose_Using_Objectdisposedexception - Fatal编程技术网

.net 警告:不要多次处置对象

.net 警告:不要多次处置对象,.net,vb.net,dispose,using,objectdisposedexception,.net,Vb.net,Dispose,Using,Objectdisposedexception,编辑: 这个问题不是重复的!,可能是一个常见的问题“此警告是什么意思?”,但当我询问如何在特定代码中修复此警告时,这并不是一个重复的问题,我自己甚至无法知道此警告的含义 VS中的代码分析向我显示以下警告: CA2202不多次处理对象对象“OutputStream”可以 在方法“filespliter.Split”(字符串,长, 字符串,字符串,布尔值,布尔值。避免产生错误 System.ObjectDisposedException调用Dispose的次数不应超过 对象上的一次:行: 490

编辑:


这个问题不是重复的!,可能是一个常见的问题“此警告是什么意思?”,但当我询问如何在特定代码中修复此警告时,这并不是一个重复的问题,我自己甚至无法知道此警告的含义


VS中的代码分析向我显示以下警告:

CA2202不多次处理对象对象“OutputStream”可以 在方法“filespliter.Split”(字符串,长, 字符串,字符串,布尔值,布尔值。避免产生错误 System.ObjectDisposedException调用Dispose的次数不应超过 对象上的一次:行: 490 WindowsApplication1 filespliter.vb 490

第490行是这样的:

End Using ' OutputStream
它对
498
行中的另一个对象给出了相同的警告,如下所示:

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

这是完整的块:

    ' 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
'打开文件开始读取字节。
将InputStream用作新的文件流(fInfo.FullName,FileMode.Open)
将BinaryReader用作新的BinaryReader(InputStream)
While(InputStream.Position
更新

添加try/catch块后,它仍然显示相同的警告。 如果我删除最后一个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
'打开文件开始读取字节。
Dim InputStream作为流=无
尝试
InputStream=新文件流(fInfo.FullName,FileMode.Open)
将BinaryReader用作新的BinaryReader(InputStream)
While(InputStream.Position
这个问题的原因在您的代码中并不明显,而只是代码分析对所涉及对象的了解

具体地说,当您构造
BinaryReader
BinaryWriter
的实例时,您将底层流的所有权交给这些对象。因此,当您处理读写器对象时,流也会被处理

同样地,当您在处理读写器对象之后继续处理底层流时,代码分析会对此发出警告

现在,这会是个问题吗?没有

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

为了“正确”地解决这个问题,通常将外部流包装在try/finally块中,只有当代码到达finally块时,才可以在没有构造读写器的情况下进行处理

换句话说,如果您实际上没有将对象的所有权授予读写器对象,那么您只希望处置底层流


我认为这个特殊的规则也给出了一个例子来说明如何做到这一点。

这个例子几乎完全模仿了你的代码。所以,如果我理解得好的话,问题是嵌套的内部
使用了
它使用了另一个
的资源使用了
,这意味着这行?:
使用BinaryWriter作为新的BinaryWriter(OutputStream)
('因为我正在向它传递
outputstream
资源,不是吗?),然后我只添加了一个try/catch块,如msdn文档中所解释的那样,感谢outer using语句末尾的帮助
,第二次释放流对象
这个问题不是重复的!可能是一个常见的问题“此警告是什么意思?”但当我询问如何在特定代码中修复此警告时,它不是重复的,我自己也不能。忽略CA2202的唯一危险是
“即使已知Dispose for the object可多次安全调用[it is],将来实现可能会更改。“
只需将
添加到该过程中,在此处忽略它。旁白:不要在捕获过程中添加新的…
,因为这会搞乱整个过程。”