Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some Cleanup Tips?

Status
Not open for further replies.

Brawn

Programmer
Jul 5, 2001
73
0
0
CA
Hi,

Just doing a little cleanup here and setting my
strings to 'vbNullString' and my
objects to 'Nothing'.

what should i set my integers to?
Maybe 'vbEmpty'?

Any other "Clean Up" information or tips is also invited.,
[neutral] Brawn

- The difference between genius and stupidity is that genius has its limits -
 
you don't need to clean up integers. String and objects variables have a referenced data type. This means that the variable only points to a certain piece of memory. I did not know that strings should be cleaned up, but it seems good practice to clean up object variables.

If you want to reset an integer variable to 'not used yet', set it to it's default value (0). [smarty]
 
You can set everything to = Nothing

Craig, mailto:sander@cogeco.ca

Remember not to name the Lambs...
It only makes the chops harder to swallow
 
My understanding is that you only need to clean up objects. Regular variables don't cause memory leaks. Is this not the case?
Something I've been wondering about: When reusing a collection, do you need to set it to Nothing before setting it as a New Collection? I've just been setting to a New Collection assuming that it didn't leave a memory void from its former life. I've seen collection clearing procedures and I wonder, why bother when you can just reset it as a new collection.
 
Objects should be set to Nothing to free all memory associated with them. Objects defined as local in a procedure will go out of scope when the procedure completes, but it good practice to explicitly set them to nothing as this can help in debugging.

Integers take a fixed amount of memory and so there is no point in cleaning them up.

Storage taken by global, module level or static strings may be freed by setting them to "" but there is no point in doing this if they are about to go out of scope anyway.

The same argument applies to dynamic arrays where the dimensions can be reduced to free storage. (I think there is a way of completely freeing the array storage, but I cannot now recall what it is).

Regarding reusing collections. I can see no reason to set the collection to nothing before reusing it.
 
Well, is setting an object to nothing really freeing memory or does it just change what the object references?
So the memory that was used by the object is just hanging out there somewhere for a while.

Maybe I just can't get rid of C++ habits?

I always have a Clear method in my objects to clean up used memory.
Code:
object.clear
then I set them to nothing.
Code:
Set object = Nothing

For collections of objects i always.
Code:
while collectionOfObjects.count > 0
   set object = collectionOfObjects(1)
   object.clear
   set object = nothing
   collectionOfObject.remove(1)
wend
set collectionOfObjects = nothing

Is all this just, well, fluff? [neutral] Brawn

- The difference between genius and stupidity is that genius has its limits -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top