Re: Can com+ Hotfix Rollup Package 27 fix my memory problem with WSH. CreateObject/CreateScript?

From: Gerry Hickman (gerry666uk_at_yahoo.co.uk)
Date: 11/24/04


Date: Wed, 24 Nov 2004 20:52:48 +0000

Al Dunbar [MS-MVP] wrote:

> Ouch! my face is still stinging from this slap, however well deserved it
> might be!

Heh, well it wasn't aimed at you:)

> I agree with *most* of what you say above (mainly the importance of
> <explicitly> closing files, recordsets, and connections), mainly because
> these activities have effects that are felt outside of the scope of a script
> by an object that has its own existence in that external domain.

Yes.

> Contrariwise, it seems to me that the de-instantiation of objects is done
> automatically by WSH. I just did a test using a class created for the
> purpose, which indicates that its terminate event is invoked by any of the
> following:

We don't have this hand-holding "terminate" event in JScript so it's the
first time I've seen it, but I don't think it changes much. You can put
your clean-up code in the "terminate" event if you like, but at the end
of the day, it's no different to calling Dispose() manually. You'd have
to do it that way in COM anyway. Of course in script, the object will be
destroyed sooner or later, but with a long script with looping you
ignore best practice at your own risk.

> to the same object, memory and system resources associated with the object
> to which the variables refer are released only after all of them have been
> set to Nothing, either explicitly using Set, or implicitly after the last
> object variable set to Nothing goes out of scope."
> Even the above definition makes no distinction between object variables
> being set to nothing and going out of scope, other than to call one explicit
> and the other implicit.

I know and I agree. I think you've looked into it in much greater depth
than I have, and I'm in no doubt your code releases everything
perfectly. However:

What I'm saying is much more basic than that. I haven't gone into
details and probably don't need to, what I'm saying is that no one needs
to worry about this, because if they follow best practice as outlined in
my earlier message, they'll never run into this problem in the first
place. Why take a risk if you don't need to? People are going to have to
program this way as soon as they look outside VBScript, so why not start
now?

As an example of not calling Dispose(), I have a class called Reg() that
is all about managing remote registries. Reading and Writing dozens of
values to hundreds of machines is a walk in the park, but what it can't
do (for performance reasons) is close the connection until I call the
destructor. If I don't call it, I'll have a gazillion network sessions
open on my network - not good. Obviously I don't want to open and close
a connection every time I read or write a key, so it's catch-22. That's
why I open the connection once, do the work, and the close it once. In
COM there's even a reference counter for this. Scripters should
implement their own reference counter and pretend they're programming in
COM.

If people don't want to follow this advice that's fine, as long as they
don't start complaining!

WSH is not as fool-proof as some people think. For example if you exec
"SLEECT * FROM CIM_DataFile" on a modern computer with a few mapped
drives, you could find yourself with a processor intensive process
running solid for over two hours - hit CTRL+C in cscript and your script
will stop (kind of), but it won't stop that process eating all your
resources for the next two hours.

-- 
Gerry Hickman (London UK)


Relevant Pages

  • Re: Name collisions in Monad
    ... We spent a lot of time working on the scoping rules in Monad. ... create a new scope when run, and variables _created_ in that new scope are ... script, call a function, or start a new instance of Windows Command ... $var is initially set to "init" in the function ...
    (microsoft.public.windows.server.scripting)
  • Re: One other related Q Re: basic Q: Only one way to make vars live outside of the scope of a functi
    ... >>> directly to the calling script, without having to use global variables ... to get multiple return items from a subprocedure. ... >>> better not to mix scope and use global variables for multiple return ... before terminating the object reference. ...
    (microsoft.public.scripting.vbscript)
  • Re: new to scripting.. the below script stops after first run..
    ... I have exports from my unix servers that aren't compatible with MS for a straight import so I've fiddled with the data, and now I want to just run the same script many times and pass it a bunch of parameters.. ... Echo Changing scope options ... Netsh dhcp server %server% scope %netid% add iprange %iprangestart% %iprangeend% BOTH> NUL ...
    (microsoft.public.windows.server.scripting)
  • Re: [MSH] WMI Help
    ... access the variable in the global scope whether or not the script is ... dot-sourced into the global scope then $script:class is equivalent to ... >> defined so it will search the stack and find it in the parent scope and ...
    (microsoft.public.windows.server.scripting)
  • Re: Event not firing properly
    ... Are the object variables and the event handler still in scope? ... Is it possible that you aren't declaring the objects at a high enough scope to keep them from being garbage collected? ... Please provide more information as to Outlook version, language and show some of the code you're using. ... If we removes our code to insert into database, ...
    (microsoft.public.office.developer.com.add_ins)