"Much Ado About Nothing"

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

"Much Ado About Nothing"

Post by ChrisGreaves »

I have just finished re-reading Mark Hurst's Excellent Article and thought to post a link here.

If you are at my level, allow ten minutes to digest it, and don't read it last thing at night. :laugh:

Here's a typical VB urban myth that crops up again and again in newsgroups:
You should always set object variables to Nothing when you're finished with them.
He who plants a seed, plants life.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: "Much Ado About Nothing"

Post by HansV »

I think this urban myth originated in a very real problem in Microsoft Access: there is or was a bug where objects of type Recordset were not destroyed at the end of a procedure if they were not set to Nothing explicitly. This had strange side effects, such as Access refusing to quit even though the database had been closed. So many programmers adopted the habit of always setting ALL object variables to Nothing, even in situations where there was no need to. In time, they forgot the original reason and it became an absolute imperative...
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: "Much Ado About Nothing"

Post by ChrisGreaves »

HansV wrote:... were not destroyed at the end of a procedure if they were not set to Nothing explicitly.
I believe you.
I found Mark Hurst's article interesting because, if I've understood it, "nothing" is not what we think it is.
It is a very real "something".
I confess that for all this time I'd thought Set obj = Nothing meant that I was destroying the object, as in deleting the symbol table entry, freeing up memory and returning it to the pool, etc etc.

"You should now realise, of course, that assigning Nothing to a pointer variable doesn't necessarily result in the object's destruction since all it does is decrement the reference count."
He who plants a seed, plants life.