简体   繁体   English

.Net:当您处理dispose()变量时,是否应该在调试器“本地”窗口中将其立即显式设置为“ Nothing”?

[英].Net: When you dispose() variable, should it immediately, visibly be set to “Nothing” in debugger Locals window?

When you dispose() a variable, should it immediately, visibly be set to "Nothing" in debugger Locals window? 当您处理()变量时,是否应该在调试器的“本地”窗口中将其立即可见地设置为“ Nothing”?

When I single step this in the Visual Studio 2010 debugger, I can see in the Locals window that the close() call does set r1's BaseStream and various other members to Nothing, but the dispose() call does NOT set the r1 variable as a whole to "Nothing". 当我在Visual Studio 2010调试器中单步执行此操作时,我可以在Locals窗口中看到close()调用确实将r1的BaseStream和其他各种成员设置为Nothing,但是dispose()调用并未将r1变量设置为a整体为“无”。 It's still listed in the Locals window as {System.IO.StreamReader}. 它仍然在“本地”窗口中列为{System.IO.StreamReader}。

Try
     r1 = New System.IO.StreamReader("c:\temp\dummy\dummy1.txt")
Finally
    If Not IsNothing(r1) Then
        r1.Close()
        r1.Dispose()
    End If
End Try

Maybe my expectations are just mismatched with how it actually works. 也许我的期望与它的实际工作方式不符。 Is there a definitive reference to explain this behavior? 是否有明确的参考资料可以解释这种行为?

EDIT: Yes, I was already aware of the Using statement, but I need to allocate two db connections, and two db commands (one for each of the opened connections). 编辑:是的,我已经知道Using语句,但是我需要分配两个数据库连接和两个数据库命令(每个打开的连接一个)。 Nesting using statements four-deep seemed convoluted. 使用四层深的语句嵌套似乎很麻烦。 Furthermore I wanted to implement a three-strikes-and-you're-out while-loop around each of the New Connection() statements, because they are prone to fail in my current environment and I'm specifically trying to troubleshoot that. 此外,我想在每个New Connection()语句周围实施三击而又不在循环中,因为在我的当前环境中它们很容易失败,因此我专门尝试解决该问题。 If someone can point me to a useful structure for blending Whiles and Usings, they get a gold star for the day. 如果有人能指出我一个有用的结构来融合Whiles和Usings,他们将成为当天的金星。

Dispose is a method like any other and it does not set the variable to Nothing (or null in C#) Dispose是一种与其他方法一样的方法,并且不会将变量设置为Nothing (在C#中为null

The purpose of the Dispose method is to "[perform] application-defined tasks associated with freeing, releasing, or resetting unmanaged resources." Dispose方法的目的是“ [执行]与释放,释放或重置非托管资源相关的应用程序定义的任务”。 ( source ) 来源

Because of that, an object is often unusable after it has been disposed but the variable it is assigned to remains unchanged until you reassign it yourself explicitely. 因此,对象在处置后通常不可用,但分配给它的变量将保持不变,直到您明确地重新分配它为止。

With that being said, you should generally use a using statement for disposable objects. 话虽如此,您通常应该对一次性对象使用using语句。

Using r1 As New System.IO.StreamReader("c:\temp\dummy\dummy1.txt")
   'Code goes here
End Using

When a variable is disposed, the object which it identifies will often become useless, but the object will still exist, and the variable will continue to identify the now-useless object until such time as either the variable is overwritten with something else or ceases to exists. 处置变量后,它标识的对象通常将变得无用,但该对象仍将存在,并且该变量将继续标识当前无用的对象,直到该变量被其他内容覆盖或停止存在。 One absolute must-not-violate-ever rule in .NET is that every object must continue to exist as long as any reachable reference to it exists. 一个绝对必须未违反有史以来在.NET中的规则是,每个对象都必须继续,只要任何可到达参考它的存在存在 The system has a means of examining every weak reference in existence, and invalidating those whose target is identified only by weak references, but there is no means by which the system could find all "normal" references that might exist to a useless object and invalidate them. 该系统可以检查存在的每个引用,并使那些由弱引用标识其目标的弱引用无效,但是系统无法通过该方法找到可能存在于无用对象的所有“正常”引用并使它们无效他们。 Thus, any references that identified an object before Dispose was called on it will continue to do so afterward unless they are explicitly overwritten by the actual code within the Dispose method itself. 因此,除非在Dispose方法本身中的实际代码明确覆盖了引用,否则在调用Dispose之前标识对象的任何引用将在之后继续这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM