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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set object = Nothing <== necessary?

Status
Not open for further replies.

krinid

Programmer
Jun 10, 2003
356
CA
Is there any necessity for executing set object = Nothing at the end of a sub when an object will go out of scope? It is of course good practice to manually free allocated memory (although even this is indirect since there's no demalloc-equivalent command).

The reason I'm asking is because in many code examples posted here, I've seen undeclared variables being used, but set object = Nothing being used to release memory in the same code sample. Kind of an inconsistent mix of style, which has got me wondering if maybe there is a reason for set objects to nothing (other than good style/practice).

According to faq707-3710, there doesn't appear to be a necessity--QUOTE:
if it is the only variable referencing the particular object, it disassociates the object from the code and releases all related system resources.
 
Hi krinid,

I guess I'd better defend my FAQ [smile].

In fairness to all it should be said that posted code snippets are not necessarily complete routines and I (and, I guess, others) may do a quick test before posting without bothering to declare a variable. I would always advocate the use of Option Explicit to impose declarations in any 'proper' application.

Setting object variables to Nothing on the other hand is not imposed by anything and is easy to forget - also questioners may not be aware of it - so it tends to get explicitly posted more frequently.

Whether or not it is necessary is a moot point. Theoretically it shouldn't be necessary but I'm never entirely convinced and prefer to be explicit and it is, as you say, good practice. Also, depending on the application, there may well be benefits in releasing resources sooner rather than later.

Enjoy,
Tony
 
Good defence of a good FAQ! Good point about releasing resources sooner than later may have some effect with some apps. Just wanted to double check my understanding. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top