Connecting Tech Pros Worldwide Forums | Help | Site Map

When to set object references = nothing

Mike Eaton
Guest
 
Posts: n/a
#1: Nov 20 '05
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

Marina
Guest
 
Posts: n/a
#2: Nov 20 '05

re: When to set object references = nothing


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" <MikeEaton@discussions.microsoft.com> wrote in message
news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=blue]
> Hi all,
>
> What do people regard as the best practice with respect to freeing object[/color]
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.[color=blue]
>
> 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[/color]
of the object reference once the procedure completes? What is the best
practice here?[color=blue]
>
> Thanks!
> Mike[/color]


Marina
Guest
 
Posts: n/a
#3: Nov 20 '05

re: When to set object references = nothing


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

"Marina" <someone@nospam.com> wrote in message
news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <MikeEaton@discussions.microsoft.com> wrote in message
> news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=green]
> > Hi all,
> >
> > What do people regard as the best practice with respect to freeing[/color][/color]
object[color=blue]
> references when you're done with them? Some people I've worked with in[/color]
the[color=blue]
> 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.[color=green]
> >
> > 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[/color]
> of the object reference once the procedure completes? What is the best
> practice here?[color=green]
> >
> > Thanks!
> > Mike[/color]
>
>[/color]


Clark Stevens
Guest
 
Posts: n/a
#4: Nov 20 '05

re: When to set object references = nothing


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" <someone@nospam.com> wrote in message
news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <MikeEaton@discussions.microsoft.com> wrote in message
> news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=green]
> > Hi all,
> >
> > What do people regard as the best practice with respect to freeing[/color][/color]
object[color=blue]
> references when you're done with them? Some people I've worked with in[/color]
the[color=blue]
> 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.[color=green]
> >
> > 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[/color]
> of the object reference once the procedure completes? What is the best
> practice here?[color=green]
> >
> > Thanks!
> > Mike[/color]
>
>[/color]


Greg Burns
Guest
 
Posts: n/a
#5: Nov 20 '05

re: When to set object references = nothing


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" <cyberbeat03@hotmail.com> wrote in message
news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=blue]
> What about if you are repeatedly creating a new reference to an object[/color]
using[color=blue]
> 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[/color]
reference?[color=blue]
>
> "Marina" <someone@nospam.com> wrote in message
> news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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" <MikeEaton@discussions.microsoft.com> wrote in message
> > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=darkred]
> > > Hi all,
> > >
> > > What do people regard as the best practice with respect to freeing[/color][/color]
> object[color=green]
> > references when you're done with them? Some people I've worked with in[/color]
> the[color=green]
> > 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[/color][/color]
whether[color=blue][color=green]
> > it's about to go out of scope or not.[color=darkred]
> > >
> > > 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[/color][/color][/color]
care[color=blue][color=green]
> > of the object reference once the procedure completes? What is the best
> > practice here?[color=darkred]
> > >
> > > Thanks!
> > > Mike[/color]
> >
> >[/color]
>
>[/color]


Greg Burns
Guest
 
Posts: n/a
#6: Nov 20 '05

re: When to set object references = nothing


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
[color=blue]
> "Clark Stevens" <cyberbeat03@hotmail.com> wrote in message
> news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=green]
> > What about if you are repeatedly creating a new reference to an object[/color]
> using[color=green]
> > 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[/color]
> reference?[color=green]
> >
> > "Marina" <someone@nospam.com> wrote in message
> > news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > 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[/color][/color][/color]
garbage[color=blue][color=green][color=darkred]
> > > collection.
> > >
> > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...
> > > > Hi all,
> > > >
> > > > What do people regard as the best practice with respect to freeing[/color]
> > object[color=darkred]
> > > references when you're done with them? Some people I've worked with[/color][/color][/color]
in[color=blue][color=green]
> > the[color=darkred]
> > > 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[/color][/color]
> whether[color=green][color=darkred]
> > > 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[/color][/color]
> care[color=green][color=darkred]
> > > of the object reference once the procedure completes? What is the[/color][/color][/color]
best[color=blue][color=green][color=darkred]
> > > practice here?
> > > >
> > > > Thanks!
> > > > Mike
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Clark Stevens
Guest
 
Posts: n/a
#7: Nov 20 '05

re: When to set object references = nothing


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:%23lVzwBndEHA.4048@TK2MSFTNGP12.phx.gbl...[color=blue]
> I assume you only have one of these in a loop (otherwise it won't[/color]
compile).[color=blue]
> 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" <cyberbeat03@hotmail.com> wrote in message
> news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=green]
> > What about if you are repeatedly creating a new reference to an object[/color]
> using[color=green]
> > 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[/color]
> reference?[color=green]
> >
> > "Marina" <someone@nospam.com> wrote in message
> > news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > 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[/color][/color][/color]
garbage[color=blue][color=green][color=darkred]
> > > collection.
> > >
> > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...
> > > > Hi all,
> > > >
> > > > What do people regard as the best practice with respect to freeing[/color]
> > object[color=darkred]
> > > references when you're done with them? Some people I've worked with[/color][/color][/color]
in[color=blue][color=green]
> > the[color=darkred]
> > > 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[/color][/color]
> whether[color=green][color=darkred]
> > > 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[/color][/color]
> care[color=green][color=darkred]
> > > of the object reference once the procedure completes? What is the[/color][/color][/color]
best[color=blue][color=green][color=darkred]
> > > practice here?
> > > >
> > > > Thanks!
> > > > Mike
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Clark Stevens
Guest
 
Posts: n/a
#8: Nov 20 '05

re: When to set object references = nothing


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" <someone@nospam.com> wrote in message
news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <MikeEaton@discussions.microsoft.com> wrote in message
> news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=green]
> > Hi all,
> >
> > What do people regard as the best practice with respect to freeing[/color][/color]
object[color=blue]
> references when you're done with them? Some people I've worked with in[/color]
the[color=blue]
> 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.[color=green]
> >
> > 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[/color]
> of the object reference once the procedure completes? What is the best
> practice here?[color=green]
> >
> > Thanks!
> > Mike[/color]
>
>[/color]


Greg Burns
Guest
 
Posts: n/a
#9: Nov 20 '05

re: When to set object references = nothing


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" <cyberbeat03@hotmail.com> wrote in message
news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=blue]
> What about if you are repeatedly creating a new reference to an object[/color]
using[color=blue]
> 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[/color]
reference?[color=blue]
>
> "Marina" <someone@nospam.com> wrote in message
> news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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" <MikeEaton@discussions.microsoft.com> wrote in message
> > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...[color=darkred]
> > > Hi all,
> > >
> > > What do people regard as the best practice with respect to freeing[/color][/color]
> object[color=green]
> > references when you're done with them? Some people I've worked with in[/color]
> the[color=green]
> > 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[/color][/color]
whether[color=blue][color=green]
> > it's about to go out of scope or not.[color=darkred]
> > >
> > > 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[/color][/color][/color]
care[color=blue][color=green]
> > of the object reference once the procedure completes? What is the best
> > practice here?[color=darkred]
> > >
> > > Thanks!
> > > Mike[/color]
> >
> >[/color]
>
>[/color]


Greg Burns
Guest
 
Posts: n/a
#10: Nov 20 '05

re: When to set object references = nothing


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
[color=blue]
> "Clark Stevens" <cyberbeat03@hotmail.com> wrote in message
> news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=green]
> > What about if you are repeatedly creating a new reference to an object[/color]
> using[color=green]
> > 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[/color]
> reference?[color=green]
> >
> > "Marina" <someone@nospam.com> wrote in message
> > news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > 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[/color][/color][/color]
garbage[color=blue][color=green][color=darkred]
> > > collection.
> > >
> > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...
> > > > Hi all,
> > > >
> > > > What do people regard as the best practice with respect to freeing[/color]
> > object[color=darkred]
> > > references when you're done with them? Some people I've worked with[/color][/color][/color]
in[color=blue][color=green]
> > the[color=darkred]
> > > 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[/color][/color]
> whether[color=green][color=darkred]
> > > 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[/color][/color]
> care[color=green][color=darkred]
> > > of the object reference once the procedure completes? What is the[/color][/color][/color]
best[color=blue][color=green][color=darkred]
> > > practice here?
> > > >
> > > > Thanks!
> > > > Mike
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Clark Stevens
Guest
 
Posts: n/a
#11: Nov 20 '05

re: When to set object references = nothing


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:%23lVzwBndEHA.4048@TK2MSFTNGP12.phx.gbl...[color=blue]
> I assume you only have one of these in a loop (otherwise it won't[/color]
compile).[color=blue]
> 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" <cyberbeat03@hotmail.com> wrote in message
> news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=green]
> > What about if you are repeatedly creating a new reference to an object[/color]
> using[color=green]
> > 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[/color]
> reference?[color=green]
> >
> > "Marina" <someone@nospam.com> wrote in message
> > news:elcguBldEHA.3612@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > 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[/color][/color][/color]
garbage[color=blue][color=green][color=darkred]
> > > collection.
> > >
> > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.com...
> > > > Hi all,
> > > >
> > > > What do people regard as the best practice with respect to freeing[/color]
> > object[color=darkred]
> > > references when you're done with them? Some people I've worked with[/color][/color][/color]
in[color=blue][color=green]
> > the[color=darkred]
> > > 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[/color][/color]
> whether[color=green][color=darkred]
> > > 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[/color][/color]
> care[color=green][color=darkred]
> > > of the object reference once the procedure completes? What is the[/color][/color][/color]
best[color=blue][color=green][color=darkred]
> > > practice here?
> > > >
> > > > Thanks!
> > > > Mike
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Mythran
Guest
 
Posts: n/a
#12: Nov 20 '05

re: When to set object references = nothing


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" <cyberbeat03@hotmail.com> wrote in message
news:s2yOc.2218$Qp.2132@twister.nyroc.rr.com...[color=blue]
> 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:%23lVzwBndEHA.4048@TK2MSFTNGP12.phx.gbl...[color=green]
> > I assume you only have one of these in a loop (otherwise it won't[/color]
> compile).[color=green]
> > 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" <cyberbeat03@hotmail.com> wrote in message
> > news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=darkred]
> > > What about if you are repeatedly creating a new reference to an object[/color]
> > using[color=darkred]
> > > 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[/color]
> > reference?[color=darkred]
> > >
> > > "Marina" <someone@nospam.com> wrote in message
> > > news:elcguBldEHA.3612@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[/color][/color]
> garbage[color=green][color=darkred]
> > > > collection.
> > > >
> > > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.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[/color][/color]
> in[color=green][color=darkred]
> > > 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[/color]
> > whether[color=darkred]
> > > > 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[/color]
> > care[color=darkred]
> > > > of the object reference once the procedure completes? What is the[/color][/color]
> best[color=green][color=darkred]
> > > > practice here?
> > > > >
> > > > > Thanks!
> > > > > Mike
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Mythran
Guest
 
Posts: n/a
#13: Nov 20 '05

re: When to set object references = nothing


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" <cyberbeat03@hotmail.com> wrote in message
news:s2yOc.2218$Qp.2132@twister.nyroc.rr.com...[color=blue]
> 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:%23lVzwBndEHA.4048@TK2MSFTNGP12.phx.gbl...[color=green]
> > I assume you only have one of these in a loop (otherwise it won't[/color]
> compile).[color=green]
> > 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" <cyberbeat03@hotmail.com> wrote in message
> > news:rIxOc.73420$yd5.62397@twister.nyroc.rr.com...[color=darkred]
> > > What about if you are repeatedly creating a new reference to an object[/color]
> > using[color=darkred]
> > > 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[/color]
> > reference?[color=darkred]
> > >
> > > "Marina" <someone@nospam.com> wrote in message
> > > news:elcguBldEHA.3612@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[/color][/color]
> garbage[color=green][color=darkred]
> > > > collection.
> > > >
> > > > "Mike Eaton" <MikeEaton@discussions.microsoft.com> wrote in message
> > > > news:6F7F5E11-8418-4009-B057-0276B1FC065A@microsoft.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[/color][/color]
> in[color=green][color=darkred]
> > > 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[/color]
> > whether[color=darkred]
> > > > 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[/color]
> > care[color=darkred]
> > > > of the object reference once the procedure completes? What is the[/color][/color]
> best[color=green][color=darkred]
> > > > practice here?
> > > > >
> > > > > Thanks!
> > > > > Mike
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread