Is it necessary to Set a Object to nothing before referencing itagain to some other object?

D

deltaquattro

Hi all,

a very important question for me: suppose I am reading some objects
into a collection. Each time I create an object and add it: since I
use always the same name for the object, do I need to set the object
to Nothing before reinstancing it or not? An example follows:

(Class1)
Public InstanceName As String

(Module1)
Option Explicit

Sub AddToCollection()
Dim MyCollection As Collection
Dim Inst As Class1, x As Class1
Dim Msg As String, TheName As String

Set MyCollection = New Collection
Do
Set Inst = New Class1 ' Create a new instance of Class1.
Msg = "Please insert a new object name " & _
"or press Cancel to exit"
TheName = InputBox(Msg, "Name the Collection Items")
Inst.InstanceName = TheName ' Put name in object instance.
' If user entered name, add it to the collection.
If Inst.InstanceName <> "" Then
' Add the named object to the collection.
MyCollection.Add Item:=Inst
End If
' Clear the current reference in preparation for next one.
Set Inst = Nothing
Loop Until TheName = ""

End Sub

If I omit the line

Set Inst = Nothing

the code works all the same. Is it correct to omit it? Or am I wasting
memory, and this could be dangerous in a bigger code? Thanks,

Best Regards

Sergio Rossi
 
B

Bob Phillips

No, there is no need to do that. All you are doing is releasing its memory
location, and then allocating a new location. If you just reuse the object,
it will grab enough memory as it needs, it may be the same or it may be
somewhere else, but the system will handle it.

The only thing you need (debatable) to do is to release the object when you
are all done with it.
 
D

deltaquattro

Thank you very much, Bob: I understand that I can safely delete the
line

Set Inst = Nothing

Two somehow related questions:

1. In the following code
Sub foo()
Dim Coll As Collection
Dim Inst As Class1

Set Coll = New Collection

MsgBox Coll.Count

Set Inst = New Class1
Inst.InstName = "test"
Coll.Add Item:=Inst

MsgBox Coll.Count

End Sub

each time I rerun the code, a new Coll object is created, as confirmed
by the fact that the first MsgBox shows 0 and the second 1. Now, are
the old objects deleted when execution is completed? Or am I filling
the memory of my PCs with many Collection objects, so I need to Set
Coll = Nothing before End Sub?

2. If I set a Collection to Nothing, is the space occcupied by it
released or not? For example,

Sub foobar()
Dim Coll As Collection
Dim Inst As Class1
Dim I As Long

Set Coll = New Collection

For I = 1 To 100000
Set Inst = New Class1
Inst.InstName = CStr(I)
Coll.Add Item:=Inst
Next I

Set Coll = Nothing

End Sub

Is the memory of the PC now filled with 100000 Objects, each
containing a string? Or not? Thanks again,

Best Regards

Sergio
 
B

Bob Phillips

deltaquattro said:
Thank you very much, Bob: I understand that I can safely delete the
line
Set Inst = Nothing

Two somehow related questions:

1. In the following code
Sub foo()
Dim Coll As Collection
Dim Inst As Class1

Set Coll = New Collection

MsgBox Coll.Count

Set Inst = New Class1
Inst.InstName = "test"
Coll.Add Item:=Inst

MsgBox Coll.Count

End Sub

each time I rerun the code, a new Coll object is created, as confirmed
by the fact that the first MsgBox shows 0 and the second 1. Now, are
the old objects deleted when execution is completed? Or am I filling
the memory of my PCs with many Collection objects, so I need to Set
Coll = Nothing before End Sub?

VB has a built-in facility called garbage collection which tidies up after
us. When an object goes out of scope, its memory is released, that is why
collection is a new object each time (actually Coll would be initialised
anyway, as you explicitly New it). So no, you are not filling memory with
many instances of that objectr, which is the point I was making in my irst
response.

Because of garbage collection, you do not need to Set Coll = Nothing,
although IMO it is a good prcatice to do so, you cannot be sure that all
languages release memory, or do it properly, and it takes little effort to
type it.

You could get paranoid, you could set all strings to "", but I don't
advocate this :)
2. If I set a Collection to Nothing, is the space occcupied by it
released or not? For example,

Sub foobar()
Dim Coll As Collection
Dim Inst As Class1
Dim I As Long

Set Coll = New Collection

For I = 1 To 100000
Set Inst = New Class1
Inst.InstName = CStr(I)
Coll.Add Item:=Inst
Next I

Set Coll = Nothing

End Sub

Yes of course, that is what that command does.
 
D

deltaquattro

VB has a built-in facility called garbage collection which tidies up after
us. When an object goes out of scope, its memory is released, that is why
collection is a new object each time (actually Coll would be initialised
anyway, as you explicitly New it). So no, you are not filling memory with
many instances of that objectr, which is the point I was making in my irst
response.

Because of garbage collection, you do not need to Set Coll = Nothing,
although IMO it is a good prcatice to do so, you cannot be sure that all
languages release memory, or do it properly, and it takes little effort to
type it.

You could get paranoid, you could set all strings to "", but I don't
advocate this :)









Yes of course, that is what that command does.
Hi Bob,

thanks a lot, it's much clearer now! Sorry if my questions can be
somewhat naive, but I just started learning OOP this year by myself,
using VBA, so there's a lot of things I don' know about objects,
collections, garbage collection (fancy name :) etc. Luckily, your
answers are helping me a lot to understand the subject :)

Ciao

deltaquattro
 

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