每个“新”是否都是“新的”。需要相应的“处理” ? [英] Does each "New" needs a corresponding "Dispose" ?

查看:59
本文介绍了每个“新”是否都是“新的”。需要相应的“处理” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在C ++中知道这是真的。但是VB .Net及其

GarbageCollector呢?


例如,请考虑以下情况。 f2()是否需要执行任何

处理代码?

Public Sub f1()As System.Text.StringBuilder

Dim obj As New System.Text.StringBuilder()

''或者,在其他情况下,这可能是任何其他类的实例

''...... />
obj.Append(" 1st ...")

obj.Append(" 2nd ...")

obj。追加(3 ...)

返回obj

结束函数


Public Sub f2()

Dim obj As System.Text.StringBuilder = Me.f1()

Me.DoSomethingWith(sb.ToString())


我需要以下几行吗?

''? obj.Dispose

''总有这样的功能...

''? obj =没什么''这需要吗?

结束Sub


谢谢

Atara。


***通过开发人员指南 http://www.developersdex.com 发送* **

不要只是参加USENET ......获得奖励!


I know in C++ it is true. but what about VB .Net and its
GarbageCollector ?

For example, consider the following case. Does f2() needs to do any
disposing code ?

Public Sub f1() As System.Text.StringBuilder
Dim obj As New System.Text.StringBuilder()
'' or, in other cases this might be an instance
'' of any other class...
obj.Append("1st ...")
obj.Append("2nd ...")
obj.Append("3rd ...")
Return obj
End Function

Public Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

'' Do I need the following lines ?
'' ? obj.Dispose
'' there is not allways such a function ...
'' ? obj = Nothing '' is this needed ?
End Sub

Thanks
Atara.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

不是没有我知道
Public Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

''我需要以下几行吗?
''? obj.Dispose
''总有这样的功能......
''? obj =没有'这是否需要?
结束子
Public Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

'' Do I need the following lines ?
'' ? obj.Dispose
'' there is not allways such a function ...
'' ? obj = Nothing '' is this needed ?
End Sub




一旦到达结束子或结束if(如果var在那里声明)

它已经自动消失


至少我认为(从来没有遇到过这样的问题......)



once you reach the end sub or the end if (if the var was declared there)
it''s gone automaticly

at least i think (never had problems w that so..)




Dispose的目的略有不同。它旨在释放

非托管资源,如文件句柄,原始GDI对象或网络

连接,而不是来自托管堆的内存。因此,如果您知道

对象使用了一些非托管资源,请调用Dispose()。


如果你不是,那么它很有可能无论如何,在垃圾回收时,对象的'

终结器将调用Dispose,但通常非托管资源是
scarse并且一个'想要尽快释放它们尽可能。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity Test Studio http://x-unity.miik.com.ua/teststudio .aspx

将单元测试的强大功能带到VS .NET IDE


Atara <在*** @ DD.com>在消息中写道

news:u4 ************** @ TK2MSFTNGP11.phx.gbl ...
Hi,

The purpose of Dispose is slightly different. It is intended to free
unmanaged resources like file handles, raw GDI objects or network
connections rather than memory from the managed heap. So if you know the
object uses some unmanaged resources, DO call Dispose().

If you don''t, it''s most likely Dispose will be called anyway by the object''s
finalizer upon garbage collection, but usually the unmanaged resources are
scarse and one''ll want to release them as soon as possible.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Atara" <At***@DD.com> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...

我用C ++知道它是真的。但是VB .Net及其GarbageCollector呢?

例如,请考虑以下情况。 f2()是否需要执行任何处理代码?

Public Sub f1()As System.Text.StringBuilder
Dim obj As New System.Text.StringBuilder()
''或者,在其他情况下,这可能是任何其他类的实例
'
obj.Append(" 1st ...")
obj .Append(2nd ...)
obj.Append(" 3rd ...")
返回obj
结束功能

公开Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

''我需要吗?以下几行?
''? obj.Dispose
''总有这样的功能......
''? obj =什么都不需要?
结束Sub

感谢
Atara。

***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET .. .get为它奖励!

I know in C++ it is true. but what about VB .Net and its
GarbageCollector ?

For example, consider the following case. Does f2() needs to do any
disposing code ?

Public Sub f1() As System.Text.StringBuilder
Dim obj As New System.Text.StringBuilder()
'' or, in other cases this might be an instance
'' of any other class...
obj.Append("1st ...")
obj.Append("2nd ...")
obj.Append("3rd ...")
Return obj
End Function

Public Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

'' Do I need the following lines ?
'' ? obj.Dispose
'' there is not allways such a function ...
'' ? obj = Nothing '' is this needed ?
End Sub

Thanks
Atara.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!






通常,Dispose用于释放''Unmanaged''资源。当有一个

dispose方法时,最好在你使用它之后调用它。在
中调用Dispose许多实例将从Finalze队列中删除Object,并且GC有更快的回收机会。

Atara <在*** @ DD.com>在消息中写道

news:u4 ************** @ TK2MSFTNGP11.phx.gbl ...
Normally, Dispose is used to release ''Unmanaged'' resources. When there is a
dispose method it is good to call it after you''v used it. Calling Dispose in
many instances will remove the Object from the Finalze queue and there is a
chance of it be reclaimed quicker by the GC.

"Atara" <At***@DD.com> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...

我用C ++知道它是真的。但是VB .Net及其GarbageCollector呢?

例如,请考虑以下情况。 f2()是否需要执行任何处理代码?

Public Sub f1()As System.Text.StringBuilder
Dim obj As New System.Text.StringBuilder()
''或者,在其他情况下,这可能是任何其他类的实例
'
obj.Append(" 1st ...")
obj .Append(2nd ...)
obj.Append(" 3rd ...")
返回obj
结束功能

公开Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

''我需要吗?以下几行?
''? obj.Dispose
''总有这样的功能......
''? obj =什么都不需要?
结束Sub

感谢
Atara。

***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET .. .get奖励它!

I know in C++ it is true. but what about VB .Net and its
GarbageCollector ?

For example, consider the following case. Does f2() needs to do any
disposing code ?

Public Sub f1() As System.Text.StringBuilder
Dim obj As New System.Text.StringBuilder()
'' or, in other cases this might be an instance
'' of any other class...
obj.Append("1st ...")
obj.Append("2nd ...")
obj.Append("3rd ...")
Return obj
End Function

Public Sub f2()
Dim obj As System.Text.StringBuilder = Me.f1()
Me.DoSomethingWith(sb.ToString())

'' Do I need the following lines ?
'' ? obj.Dispose
'' there is not allways such a function ...
'' ? obj = Nothing '' is this needed ?
End Sub

Thanks
Atara.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



这篇关于每个“新”是否都是“新的”。需要相应的“处理” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆