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

When to set object references = nothing

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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

Nov 20 '05 #2

P: n/a
I meant, when DoSomething exits, not exists...

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 20 '05 #3

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

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 20 '05 #4

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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?
"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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



Nov 20 '05 #5

P: n/a
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
"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> 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



Nov 20 '05 #6

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

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> 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



Nov 20 '05 #7

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

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 20 '05 #8

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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?
"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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



Nov 20 '05 #9

P: n/a
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
"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> 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



Nov 20 '05 #10

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

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> 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



Nov 20 '05 #11

P: n/a
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.ReleaseComO bject() 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
"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:s2****************@twister.nyroc.rr.com...
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?

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
> 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" <Mi*******@discussions.microsoft.com> wrote in message
> news:6F**********************************@microsof t.com...
> > 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
>
>



Nov 20 '05 #12

P: n/a
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.ReleaseComO bject() 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
"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:s2****************@twister.nyroc.rr.com...
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?

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Clark Stevens" <cy*********@hotmail.com> wrote in message
news:rI*******************@twister.nyroc.rr.com...
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?

"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
> 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" <Mi*******@discussions.microsoft.com> wrote in message
> news:6F**********************************@microsof t.com...
> > 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
>
>



Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.