By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,172 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,172 IT Pros & Developers. It's quick & easy.

Does each "New" needs a corresponding "Dispose" ?

P: n/a

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!
Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
no not that i know of
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


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..)
Nov 20 '05 #2

P: n/a
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...

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!


Nov 20 '05 #3

P: n/a
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...

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!

Nov 20 '05 #4

P: n/a
"Atara" <At***@DD.com> wrote...

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


Rather than rely on a yes/ no answer in a newsgroup you probably should read
up on Dispose() and Finalize(). They don't have to be called but they are
there for a reason and (depending upon what your code is doing) can have an
impact.

Tom
Nov 20 '05 #5

P: n/a
"Atara" <At***@DD.com> schrieb

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


You don't need both lines. obj.Dispose does not exist, and obj = Nothing is
not necessary because the variable runs out of scope anyway.

see also:
http://msdn.microsoft.com/library/en...collection.asp

especially subtopic Cleaning Up Unmanaged Resources->Implementing a Dispose Method

and:
http://msdn.microsoft.com/library/en...management.asp

--
Armin

Nov 20 '05 #6

P: n/a
Hi Atara,

Generally speaking you no longer need to call Dispose because the GC will
do that for you. However there are two 'But's.

The first is that it is not guaranteed that the GC will get that far.

The second is that it will do it when it is ready and any resources tied
up in the object are held on to until then. In the worst case, you may run out
of resources even though you no longer using any - because they are still
being retained.

Dispose isn't implemented in many, if not most, objects because it is not
needed. Others have a different name, eg 'Close'. These functions are provided
so that the programmer can relese the resources at the earliest opportunity.
Finished with a file? - call Close. Done with your registryKey? - call Close.
Finished drawing to the Graphics that you've created? call Dispose, etc.

It's not essential, but if a Close or Dispose has been provided, it's a
good idea to use it.

Regards,
Fergus
Nov 20 '05 #7

P: n/a
Cor
Nice answer Dmitriy
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.


Short and and in my eyes good understandable.

Cor
Nov 20 '05 #8

P: n/a
* "Armin Zingler" <az*******@freenet.de> scripsit:
You don't need both lines. obj.Dispose does not exist, and obj = Nothing is
not necessary because the variable runs out of scope anyway.


I remember that somebody from MSFT recently said that objects should not
be set to 'Nothing'. I don't remember why but I will post the link when
I find it...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #9

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Armin Zingler" <az*******@freenet.de> scripsit:
You don't need both lines. obj.Dispose does not exist, and obj =
Nothing is not necessary because the variable runs out of scope
anyway.


I remember that somebody from MSFT recently said that objects should
not be set to 'Nothing'. I don't remember why but I will post the
link when I find it...


You're right, I remember too, there was a discussion some weeks/month ago,
but I didn't pay attention on it closer.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #10

P: n/a
In article <eI**************@TK2MSFTNGP10.phx.gbl>, Armin Zingler wrote:
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Armin Zingler" <az*******@freenet.de> scripsit:
> You don't need both lines. obj.Dispose does not exist, and obj =
> Nothing is not necessary because the variable runs out of scope
> anyway.


I remember that somebody from MSFT recently said that objects should
not be set to 'Nothing'. I don't remember why but I will post the
link when I find it...


You're right, I remember too, there was a discussion some weeks/month ago,
but I didn't pay attention on it closer.


I don't have a link, but I think this has to do with a compiler
optimization that allows an object to be released to GC even if there is
an active reference to it. Basically, if you are in a method and GC
runs, and a local object is not referenced in the code any more, then
the object will be collected - even if there is a variable with an valid
refence to it. If you are one of those people that likes to set objects
to nothing just before a procedure exits, it makes this optimization
impossible, because you are referencing the object in code again... I'm
not sure if that is clear, but here is a little code:

Public Sub MyMethod()
Dim obj As New SomeObject()

' Do A bunch of stuff with obj
obj.DoSomeWork()

' Do A bunch of Stuff that doesn't ref obj anymore
DoSomeMoreWork() ' GC Runs here, so obj can be released

' unless you do this:
obj = Nothing
' Now obj waits for the next GC

End Sub

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #11

P: n/a
Hi Tom,

Here's that conversation in our group with a link to the original (in the performance group) with David Notario from
the Microsoft JIT compiler team
http://tinyurl.com/tz2v

He said what you've just said, then he confused the issue by saying that the compiler will optimise that null
assignment away!

Regards,
Fergus
Nov 20 '05 #12

P: n/a
In article <ee**************@TK2MSFTNGP12.phx.gbl>, Fergus Cooney wrote:
Hi Tom,

Here's that conversation in our group with a link to the original (in the performance group) with David Notario from
the Microsoft JIT compiler team
http://tinyurl.com/tz2v

He said what you've just said, then he confused the issue by saying that the compiler will optimise that null
assignment away!


That does confuse it doesn't it... I just remeber having this
discussion with someone during either the beta or rc period.
Maybe things have changed. It seems to me, that it should be able to
do that, though. Oh, well...

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #13

P: n/a
* "Fergus Cooney" <fi****@post.com> scripsit:
Here's that conversation in our group with a link to the original (in the performance group) with David Notario from
the Microsoft JIT compiler team
http://tinyurl.com/tz2v


Ah... thanks. That's the article I was talking about...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #14

P: n/a

Thanks for the detailed answer.

Atara.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.