When to set object references = nothing

G

Guest

Hi all,

What do people regard as the best practice with respect to freeing object references when you're done with them? Some people I've worked with in the past suggested that if you create an object with "New" then you should free/set it to nothing when you're finished with it, regardless of whether it's about to go out of scope or not.

In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
'... do some things with this form
fForm.Close()
fForm = Nothing
End Sub

Is the "fForm = Nothing" necessary if garbage collection will take care of the object reference once the procedure completes? What is the best practice here?

Thanks!
Mike
 
M

Marina

You don't need to set it to nothing. When DoSomething exists, all the
variables defined in it go out of scope and become eligible for garbage
collection.

Mike Eaton said:
Hi all,

What do people regard as the best practice with respect to freeing object
references when you're done with them? Some people I've worked with in the
past suggested that if you create an object with "New" then you should
free/set it to nothing when you're finished with it, regardless of whether
it's about to go out of scope or not.
In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
'... do some things with this form
fForm.Close()
fForm = Nothing
End Sub

Is the "fForm = Nothing" necessary if garbage collection will take care
of the object reference once the procedure completes? What is the best
practice here?
 
M

Marina

I meant, when DoSomething exits, not exists...

Marina said:
You don't need to set it to nothing. When DoSomething exists, all the
variables defined in it go out of scope and become eligible for garbage
collection.

object
references when you're done with them? Some people I've worked with in the
past suggested that if you create an object with "New" then you should
free/set it to nothing when you're finished with it, regardless of whether
it's about to go out of scope or not.
of the object reference once the procedure completes? What is the best
practice here?
 
C

Clark Stevens

What about if you are repeatedly creating a new reference to an object using
the same object variable each time? For example:

Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text = "First ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing

Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text = "Another ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing

Is it necessary to set lstStuff to nothing before creating a New reference?
 
G

Greg Burns

I assume you only have one of these in a loop (otherwise it won't compile).
I would assume on the next iteration of the loop that lstSutff variable
would have gone out of scope, ready for garabase collection.

Greg
 
G

Greg Burns

After rereading, I think you meant to type something more akin to this:

Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text = "First ListviewItem"
listview1.Items.Add(lstStuff)

lstStuff = New ListViewItem
lstStuff.Text = "Another ListviewItem"
listview1.Items.Add(lstStuff)

Again, I don't see any need to set it to nothing.

Greg
 
C

Clark Stevens

Sorry, I'm an idiot!. My sample should have been:

Dim lstStuff AS ListViewItem

lstStuff = New ListViewItem
lstStuff.Text = "First ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing

lstStuff = New ListViewItem
lstStuff.Text = "Another ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing

Do I need to set lstStuff to NOTHING in this example?
 
M

Mythran

Well, first of all, it depends :p

In your example...no, you don't need to set the value to Nothing....although..in
a situation I was just in, I did need to call
System.Runtime.InteropServices.Marshal.ReleaseComObject() to release a GroupWise
object when I used it in ASP.Net. But that's another story...other than
unmanaged objects, you don't have to worry about free'ing the references. The
garbage collection is pretty good at what it does for every day tasks.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top