A question about Nothing

A question about Nothing

Anonymous
Not applicable
522 Views
14 Replies
Message 1 of 15

A question about Nothing

Anonymous
Not applicable
Hi

I understand what Nothing does:

Set MyObject = Nothing

But I don't understand the reason for it.
Especially at the end of a routine

Doesn't End Sub do the same thing?

Dave F.
0 Likes
523 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable
I believe it unloads it from memory. I may be wrong though.

Tim Riley


"Dave F" wrote in message
news:1789035C898EEE36465216489561B600@in.WebX.maYIadrTaRb...
> Hi
>
> I understand what Nothing does:
>
> Set MyObject = Nothing
>
> But I don't understand the reason for it.
> Especially at the end of a routine
>
> Doesn't End Sub do the same thing?
>
> Dave F.
>
>
0 Likes
Message 3 of 15

Anonymous
Not applicable
Dave F wrote:

> But I don't understand the reason for it.
> Especially at the end of a routine
>
> Doesn't End Sub do the same thing?

It's insurance. While VB/A is quite good at cleaning up after itself, it
doesn't hurt for *you* to clean up afte yourself. It also makes the
object's life cycle quite obvious which is handy during debugging.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
0 Likes
Message 4 of 15

Anonymous
Not applicable
"Dave F" wrote in message news:1789035C898EEE36465216489561B600@in.WebX.maYIadrTaRb...
> Hi
>
> I understand what Nothing does:
>
> Set MyObject = Nothing
>
> But I don't understand the reason for it.
> Especially at the end of a routine
>
> Doesn't End Sub do the same thing?

Yes, and doing it explicitly is entirely pointless.

The only time that you need to explicitly set a
local object variable to Nothing, is if the code
that follows depends on a side-effect of the object
destroying itself, or when you want to control the
order in which multiple, interdependent objects are
destroyed.

A good example of this would be an ObjectDBX document
which you need to move/rename after accessing it via
ObjectDBX. Since the only way to release the lock on
the .DWG file is by destroying the AxDbDocument object,
if you wanted to rename the .DWG file after you've
finished accessing it, you would first have to set the
AxDbDocument variable to Nothing, to release the lock
on the file.

--
AcadXTabs: Document Tabs for AutoCAD
http://www.acadxtabs.com
0 Likes
Message 5 of 15

Anonymous
Not applicable
Hi Tony,
Since you're talking about destroying files by setting to nothing, I had a
related question on another thread.
if I were to open a file thus

sResult=
CreateObject("Scripting.FileSystemObject").OpenTextFile(fname).ReadAll

does that open file handle get closed when the sub terminates and sResult
goes out of scope? or maybe even immediatly after .Readall???
or is there somehow a transient bit of memory floating around that still has
the file open? also curious about the fso object that was created, since
theres no variable pointing to it, how and when is it released? magically
and instantly as soon as it's done it's work?

it doesn't put a lock on the file so there's no problem with subsequently
opening it in for example, notepad, but i was just curious about a second
opinion.

"Tony Tanzillo" wrote in message
news:DD6601BF9312A0A9DCC34B59971E34F3@in.WebX.maYIadrTaRb...
> "Dave F" wrote in message
news:1789035C898EEE36465216489561B600@in.WebX.maYIadrTaRb...
> > Hi
> >
> > I understand what Nothing does:
> >
> > Set MyObject = Nothing
> >
> > But I don't understand the reason for it.
> > Especially at the end of a routine
> >
> > Doesn't End Sub do the same thing?
>
> Yes, and doing it explicitly is entirely pointless.
>
> The only time that you need to explicitly set a
> local object variable to Nothing, is if the code
> that follows depends on a side-effect of the object
> destroying itself, or when you want to control the
> order in which multiple, interdependent objects are
> destroyed.
>
> A good example of this would be an ObjectDBX document
> which you need to move/rename after accessing it via
> ObjectDBX. Since the only way to release the lock on
> the .DWG file is by destroying the AxDbDocument object,
> if you wanted to rename the .DWG file after you've
> finished accessing it, you would first have to set the
> AxDbDocument variable to Nothing, to release the lock
> on the file.
>
> --
> AcadXTabs: Document Tabs for AutoCAD
> http://www.acadxtabs.com
>
>
>
0 Likes
Message 6 of 15

Anonymous
Not applicable
Tony Tanzillo wrote:

> Yes, and doing it explicitly is entirely pointless.
>
> The only time that you need to explicitly set a
> local object variable to Nothing, is if the code
> that follows depends on a side-effect of the object
> destroying itself, or when you want to control the
> order in which multiple, interdependent objects are
> destroyed.

So you're saying it's *not* entirely pointless?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
0 Likes
Message 7 of 15

Anonymous
Not applicable
"Mark Propst" wrote in message news:3898213F33E7FEE87FCE0E03B8ADA2DD@in.WebX.maYIadrTaRb...

> Hi Tony,
> Since you're talking about destroying files by setting to nothing, I had a
> related question on another thread.
> if I were to open a file thus
>
> sResult=
> CreateObject("Scripting.FileSystemObject").OpenTextFile(fname).ReadAll
>
> does that open file handle get closed when the sub terminates and sResult
> goes out of scope? or maybe even immediatly after .Readall???


OpenTextFile returns a TextStream object, which has a Close()
method. My guess is that the file is closed when the TextStream
object is destroyed, but don't hold me to it.

--
AcadXTabs: Document Tabs for AutoCAD
http://www.acadxtabs.com


>
0 Likes
Message 8 of 15

Anonymous
Not applicable
> sResult=
> CreateObject("Scripting.FileSystemObject").OpenTextFile(fname).ReadAll
>
> does that open file handle get closed when the sub terminates and sResult
> goes out of scope? or maybe even immediatly after .Readall???

Just checked, and the file is indeed closed automatically
when the TextStream object is destroyed.

--
AcadXTabs: Document Tabs for AutoCAD
http://www.acadxtabs.com
0 Likes
Message 9 of 15

Anonymous
Not applicable
If you dont do this , MyObject will still hold referance to the object.

To check it create an AcadApplication object and dont use Set MyObject =
Nothing at the end.

when u shutdown the machine, it will popup a dialog box indicating the
object is still being referred to.





"Dave F" wrote in message
news:1789035C898EEE36465216489561B600@in.WebX.maYIadrTaRb...
> Hi
>
> I understand what Nothing does:
>
> Set MyObject = Nothing
>
> But I don't understand the reason for it.
> Especially at the end of a routine
>
> Doesn't End Sub do the same thing?
>
> Dave F.
>
>
0 Likes
Message 10 of 15

Anonymous
Not applicable
Do you have an example?
0 Likes
Message 10 of 15

Anonymous
Not applicable
Do you have an example?
0 Likes
Message 12 of 15

Anonymous
Not applicable
Thanks Tony,
just curious, how did you check that the file was closed?
since it wasn't set to a variable I couldn't think of a way to know if a
copy of it was still being held in memory or not.?
is there a way to actually "look"at memory to see what's there?

"Tony Tanzillo" wrote in message
news:80A8283AF86BC15ED00F18BD3687D030@in.WebX.maYIadrTaRb...
> > sResult=
> > CreateObject("Scripting.FileSystemObject").OpenTextFile(fname).ReadAll
> >
> > does that open file handle get closed when the sub terminates and
sResult
> > goes out of scope? or maybe even immediatly after .Readall???
>
> Just checked, and the file is indeed closed automatically
> when the TextStream object is destroyed.
>
> --
> AcadXTabs: Document Tabs for AutoCAD
> http://www.acadxtabs.com
>
>
>
0 Likes
Message 13 of 15

Anonymous
Not applicable
I'm guessing this would tell. I haven't tried it myself.

http://www.sysinternals.com/ntw2k/freeware/handle.shtml

LJB
0 Likes
Message 14 of 15

Anonymous
Not applicable
Actually this one is better

http://www.sysinternals.com/ntw2k/freeware/procexp.shtml

I tried it but failed to detect the file I used opening. Probably because
the duration was too short.

LJB
0 Likes
Message 15 of 15

Anonymous
Not applicable
cool, thanks
Mark

"ljb" <.> wrote in message
news:0159983D3BDF7982237CDD6ABD6DF960@in.WebX.maYIadrTaRb...
> I'm guessing this would tell. I haven't tried it myself.
>
> http://www.sysinternals.com/ntw2k/freeware/handle.shtml
>
> LJB
>
>
0 Likes