Setting objects equal to Nothing-- when to do? 
Author Message
 Setting objects equal to Nothing-- when to do?

    I've got some custom classes in an ActiveX DLL, which I call in another
DLL like so:

    Private myFunction() as Integer

            Dim objSomething As MyDll.MyClass

            Set objSomething = New MyDll.MyClass

            'use the object here

            myFunction = someInteger

    Exit Function

    Now, I'm wondering if I should set my "objSomething"  = to Nothing
before I exit the function, and if I don't will memory leaks result?  If
this were C++ it'd be a no-brainer to use delete, but VB managing memory
allocation behind the scenes... I'm not as confident as to what's the right
way of going about things

      I ask because I'm using some code from a VB book, and the author isn't
setting any objects equal to Nothing when he's done with them.  Also, does
it matter if I say that these objects are running under a COM+ package?
Thanks for the help.



Wed, 15 Sep 2004 01:33:49 GMT  
 Setting objects equal to Nothing-- when to do?

Quote:

>     Now, I'm wondering if I should set my "objSomething"  = to Nothing
> before I exit the function, and if I don't will memory leaks result?  If
> this were C++ it'd be a no-brainer to use delete, but VB managing memory
> allocation behind the scenes... I'm not as confident as to what's the right
> way of going about things

It only really matters if you're using ActiveX components written
in MSVC+ and ATL, which throws too much of the burden back on the
component writers, so of course they occasionally goof.  Previous
versions of DAO and ADODB, for example, were notorious for memory
leaks if you didn't both .Close and explicitly Set the objects to
Nothing, and in exactly the right order, which could change for
mysterious reasons.  Pretty nice, huh?

--
Joe Foster <mailto:jlfoster%40znet.com>  "Regged" again? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Wed, 15 Sep 2004 01:52:14 GMT  
 Setting objects equal to Nothing-- when to do?
The COM+ question, I can't help you with... but you won't need to set the
object = nothing if the variable referencing it is private to that
procedure... proof below...
'============Form Code
Option Explicit

Private Sub Command1_Click()
   Dim o As Class1

   'Stop and single step from here
   Debug.Assert False

   'Watch for Class_Initialize
   'when the line below runs
   Set o = New Class1

   'Watch for Class_Terminate
   'when the line below runs
End Sub
'============Class1 Code
Option Explicit

Private Sub Class_Initialize()
   Debug.Print "Class1:Class_Initialize", Timer
End Sub

Private Sub Class_Terminate()
   Debug.Print "Class1:Class_Terminate", Timer
End Sub
'============

--
Ken Halter
MS-MVP-VB
Please keep it in the groups..


Quote:

>     I've got some custom classes in an ActiveX DLL, which I call in
another
> DLL like so:

>     Private myFunction() as Integer

>             Dim objSomething As MyDll.MyClass

>             Set objSomething = New MyDll.MyClass

>             'use the object here

>             myFunction = someInteger

>     Exit Function

>     Now, I'm wondering if I should set my "objSomething"  = to Nothing
> before I exit the function, and if I don't will memory leaks result?  If
> this were C++ it'd be a no-brainer to use delete, but VB managing memory
> allocation behind the scenes... I'm not as confident as to what's the
right
> way of going about things

>       I ask because I'm using some code from a VB book, and the author
isn't
> setting any objects equal to Nothing when he's done with them.  Also, does
> it matter if I say that these objects are running under a COM+ package?
> Thanks for the help.



Wed, 15 Sep 2004 04:12:13 GMT  
 Setting objects equal to Nothing-- when to do?
Jim,

Technically you don't have to set objects = Nothing because VB will do it
for you. When an object goes out of scope VB decrements the reference count
for you and when the reference count gets to 0 the object's terminate event
fires and it is destroyed.

With that said I still suggest that you explicitly set your objects to
Nothing when you are done with them for two reasons. First, it helps your
code be self documenting, by setting the object to Nothing you are telling
someone reading your code that you are finished with this object. Second, it
gets you in the habit so you are more prepared to move to VB.NET. Without
getting into a discussion of how .NET disposes of objects (garbage
collection) versus how VB 6 does it (reference counting) you should be aware
that many objects in .NET require you to explicitly call their Dispose
method so they get cleaned up correctly. If you consistently set your object
to Nothing in VB 6 you can just add the call to Dispose in VB.NET when it is
required.

--
Rob Windsor
G6 Consulting
Toronto, Canada


Quote:

>     I've got some custom classes in an ActiveX DLL, which I call in
another
> DLL like so:

>     Private myFunction() as Integer

>             Dim objSomething As MyDll.MyClass

>             Set objSomething = New MyDll.MyClass

>             'use the object here

>             myFunction = someInteger

>     Exit Function

>     Now, I'm wondering if I should set my "objSomething"  = to Nothing
> before I exit the function, and if I don't will memory leaks result?  If
> this were C++ it'd be a no-brainer to use delete, but VB managing memory
> allocation behind the scenes... I'm not as confident as to what's the
right
> way of going about things

>       I ask because I'm using some code from a VB book, and the author
isn't
> setting any objects equal to Nothing when he's done with them.  Also, does
> it matter if I say that these objects are running under a COM+ package?
> Thanks for the help.



Sat, 18 Sep 2004 01:10:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ADO Connection Object Close and set it equal to nothing

2. How to test if an object equals nothing

3. How to test if an object equals nothing

4. Determining If An Object Is Equal To Nothing

5. Testing an Object equal to Nothing

6. To set = nothing or not to set = nothing...

7. Setting an object variable to Nothing does not destroy the object

8. Why setting objects to Nothing?

9. Setting Objects to Nothing

10. Setting Objects to Nothing

11. Setting objects to Nothing

12. Either Nothing Or Object Reference Not Set

 

 
Powered by phpBB® Forum Software