What is the order in which objects are destroyed in VBScript?

In what order are objects in .vbs destroyed?

That is, given these global variables:

Set x = New Xxx Set y = New Yyy 

I'm interested in answers to any of the following.

  • For class instances implemented in .VBS, in what order will Class_Terminate be called? Fugitive coquetry assumes in the order (not the reverse order!) Of Creation, but is it guaranteed?

    EDIT : I understand that Class_Terminate will be called when the last last object reference is issued. I meant: in what order will x and y be released, and is this guaranteed? For simplicity, suppose that x and y are the only references to their respective objects.

  • Does the type of object matter? for example, if I have classes embedded in .VBS mixed with other COM objects such as Scripting.FileSystemObject .

    EDIT : I understand that the COM library can configure its own internal circular references that the host script engine does not know about; I am interested to learn what might affect the answer to the first question.

  • Are the answers to the above different if x and y are local to Sub or Function and not global?
  • It depends on whether the output is normal, by exception, or through WScript.Quit ? (In the latter case, it seems that Class_Terminate still calls any incomplete objects before exiting, however this may cause an error message).
  • When is a WScript object destroyed?
  • Is there a host script? (wscript.exe vs cscript.exe compared to any web host engine)
  • Is the JScript object destruction model different from VBScript?

I can find answers to some of these questions empirically, but I'm wondering if any of them are guaranteed / documented.

Publishing, even if you know only some of the answers - or other important issues.

+4
source share
1 answer

I developed and implemented this function in VBScript.

Most of the answers are given in my articles that link to Mark, but just for clarification:

in what order will Class_Terminate be called?

Terminators are usually called immediately when the last reference to the object is called. However, because of circular links and other issues, it is usually a very bad idea to rely on a determinate termination order.

A restless phrase suggests in the order (and not the reverse order!) Of creation, but is it guaranteed?

As I already noted in my articles, unused objects stop when the engine is turned off. As an implementation detail, the completion queue is executed in the order in which the objects were created. However, this is an undocumented implementation detail that you cannot rely on.

Is there an object type? for example, if I have classes embedded in .VBS mixed with other COM objects such as Scripting.FileSystemObject.

He can. There may be circular links among those objects that break down at unpredictable times.

I think of objects in the global scope when the program exits - it is different from objects, for example. function?

I do not understand the question. Can you clarify?

Does it depend on whether the output is normal, by exception or using WScript.Quit? (In the latter case, it seems that Class_Terminate still calls any incomplete objects before exiting, however this may cause an error message).

It may matter, yes. VBScript does not guarantee that terminators will always work. The host that owns the engine can terminate its process in a “failed” way, for example, without guaranteeing a clean shutdown of the engine. (In the event of a catastrophic failure, this is sometimes desirable, if you don’t know what’s wrong, sometimes running the completion code makes the problem worse, not better.)

Windows Script The host is trying to completely disable the engine when calling Quit.

When is a WScript object destroyed?

When the Windows Script Host process termination logic is executed.

Is there a script host? (wscript.exe vs cscript.exe compared to any web host engine)

Yes, that can make a difference.

Is the JScript object destruction model different from VBScript?

Yes very.

JScript "Classic" from the period when I worked on it (until 2001) uses a non-deterministic marking and markup garbage collector that processes circular links among Script objects, but DOES NOT process circular links between Script and browser objects. Later versions of JScript "Classic" have a modified garbage collector that handles circular references between Script and browser objects (although it does not necessarily detect circular values ​​associated with JScript objects and third-party ActiveX objects.)

IE 9 JScript has a completely rewritten garbage collector that uses very different technologies; I chatted a bit with his designer, but I do not have enough technical knowledge to discuss its characteristics at any depth.

JScript.NET, of course, uses the CLR garbage collector.

May I ask why you care about all this?

Also note that I have not watched this code for more than ten years; take it all with an appropriate level of skepticism. My memory may be malfunctioning.

+8
source

Source: https://habr.com/ru/post/1311214/


All Articles