在VBScript中销毁对象的顺序是什么? [英] What is the order of destruction of objects in VBScript?

查看:113
本文介绍了在VBScript中销毁对象的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.vbs中的对象按什么顺序被破坏?

In what order are objects in a .vbs destroyed?

也就是说,鉴于这些全局变量:

That is, given these globals:

Set x = New Xxx
Set y = New Yyy

我对以下任何一个。

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


  1. 对于在.VBS中实现的类的实例, Class_Terminate 被调用?游标戳按创建的顺序(不是相反的顺序!)建议,但是可以保证吗?

  1. For instances of classes implemented in the .VBS, in what order will Class_Terminate be called? Cursory poking suggests in the order (not reverse order!) of creation, but is this guaranteed?

EDIT :我知道 Class_Terminate 将在释放对对象的最后一个引用时被调用。我的意思是:x和y会以什么顺序释放,并保证得到释放?为简单起见,假设x& y是对其各自对象的唯一引用。

EDIT: I understand that Class_Terminate will be called when the last last reference to an object is released. What I meant was: in what order will x and y be released, and is it guaranteed? Assume for simplicity that x & y are the only references to their respective objects.

对象的类型重要吗?例如如果我在.VBS中实现的类与其他COM对象(例如 Scripting.FileSystemObject )混合在一起。

Does the type of object matter? e.g. if I have classes implemented in the .VBS mixed in with other COM objects such as Scripting.FileSystemObject.

EDIT :我了解COM库可能会设置自己的内部循环引用,脚本宿主引擎对此一无所知。我有兴趣探索什么会影响第一个问题的答案。

EDIT: I understand that a COM library may set up its own internal circular references that the script host engine knows nothing about; I'm interested in exploring what could affect the answer to the first question.

如果上述不同,答案是否相同? x和y是Sub或Function的局部变量,而不是全局变量?

Are the answers to the above different if x and y were local to a Sub or Function rather than global?

这是否取决于退出是否正常,异常还是通过 WScript.Quit 吗? (在后一种情况下,在退出前,似乎仍对任何未完成的对象调用 Class_Terminate ,但是这些可能会导致报告错误。)

Does it depend on whether the exit is normal, by exception, or via WScript.Quit? (In the latter case, it seems that Class_Terminate is still called on any outstanding objects before exiting, however these may cause an error to be reported).

何时销毁WScript对象?

When is the WScript object destroyed?

脚本宿主很重要吗? ? (wscript.exe与cscript.exe以及任何称为Web主机引擎的东西)

Does the script host matter? (wscript.exe vs cscript.exe vs. whatever the web host engine is called)

JScript的对象销毁模型与VBScript的不同吗? / p>

Does JScript's object destruction model differ to VBScript's?

我可以凭经验找到其中一些问题的答案,但我对其中任何一个是否感兴趣保证 /已记录。

I can find the answers to some of these questions empirically, but I'm interested in whether any of them are guaranteed / documented.

即使您只知道某些答案或其他相关问题,也要发帖。

Do post even if you only know some of the answers - or further relevant issues.

推荐答案

我在VBScript中设计并实现了此功能。

I designed and implemented this feature in VBScript.

大多数答案都在Mark提到的我的文章中,但只是为了澄清一下:

Most of the answers are in my articles that Mark references, but just to clarify:


将以什么顺序调用Class_Terminate?

in what order will Class_Terminate be called?

通常在释放对对象的最后一个引用时立即调用终结符。但是,由于循环引用和其他问题,依靠确定性的终止顺序通常是一个非常不好的主意

Terminators are in general called immediately when the last reference to an object is released. However, due to circular references and other issues, it is generally a very bad idea to rely upon a deterministic order of termination.


按顺序进行戳戳建议创建的顺序(不是反向顺序!),但是可以保证吗?

Cursory poking suggests in the order (not reverse order!) of creation, but is this guaranteed?

在我的文章中提到,当引擎关闭时,未终止的对象将终止。作为实现的详细信息,终止队列按创建对象的顺序执行。但是,这是您不应该依赖的未记录的实现详细信息。

As I noted in my articles, unterminated objects are terminated when the engine is shut down. As an implementation detail, the termination queue is executed in the order that the objects were created in. However, this is an undocumented implementation detail that you should not rely upon.


对象的类型重要吗?例如如果我在.VBS中实现的类与其他COM对象(例如Scripting.FileSystemObject)混合在一起。

Does the type of object matter? e.g. if I have classes implemented in the .VBS mixed in with other COM objects such as Scripting.FileSystemObject.

可以。在那些无法预测的时间被拆除的对象中可能会有循环引用。

It can. There could be circular references amongst those objects that are torn down at unpredictable times.


我正在考虑全局范围内的对象,当程序退出-例如,对象是否不同功能范围?

I'm thinking of objects at global scope, when the program quits - is it different for objects at e.g. function scope?

我不明白这个问题。您能澄清一下吗?

I don't understand the question. Can you clarify?


这是否取决于退出是正常的,例外的还是通过WScript.Quit的? (在后一种情况下,似乎在退出任何未完成的对象之前仍对Class_Terminate进行调用,但是这些对象可能会导致报告错误。)

Does it depend on whether the exit is normal, by exception, or via WScript.Quit? (In the latter case, it seems that Class_Terminate is still called on any outstanding objects before exiting, however these may cause an error to be reported).

这很重要,是的。 VBScript不能保证终止程序始终运行。拥有引擎的主机可以通过快速失败来关闭进程,例如,不能保证完全关闭引擎。 。 (如果发生灾难性的故障,这有时是可取的;如果您不知道出了什么问题,那么有时运行终止代码会使问题更严重,而不是更好。)

It can matter, yes. VBScript does not make any guarantee that terminators always run. The host that owns the engine can shut down its process by "failing fast" in a manner that is not guaranteed to cleanly shut down the engine, for example. (In the event of a catastrophic failure, this is sometimes desirable; if you don't know what is wrong then sometimes running termination code makes the problem worse, not better.)

在调用Quit时,Windows Script Host确实尝试干净地关闭引擎。

Windows Script Host does attempt to shut down the engine cleanly when Quit is called.


WScript对象何时被销毁?

When is the WScript object destroyed?

Windows脚本宿主进程终止逻辑运行时。

When the Windows Script Host process termination logic runs.


脚本宿主重要吗? (wscript.exe与cscript.exe以及任何称为Web主机引擎的东西)

Does the script host matter? (wscript.exe vs cscript.exe vs. whatever the web host engine is called)

是的,这很重要。


JScript的对象销毁模型与VBScript的不同吗?

Does JScript's object destruction model differ to VBScript's?



Yes, very much so.

我工作期间(2001年之前)的JScript经典使用了不确定的标记清除垃圾收集器,该收集器确实处理了脚本对象之间的循环引用,但不处理脚本和浏览器对象之间的循环引用。较新版本的JScript Classic具有经过修改的垃圾收集器,该DOES处理脚本和浏览器对象之间的循环引用(尽管它不一定检测到涉及JScript对象和第三方ActiveX对象的循环性。)

JScript "Classic" from the period when I worked on it (pre 2001) uses a nondeterministic mark-and-sweep garbage collector which does handle circular references amongst script objects, but does NOT handle circular references between script and browser objects. More recent versions of JScript "Classic" have a modified garbage collector that DOES handle circular references between script and browser objects (though it does not necessarily detect circularities involving JScript objects and third party ActiveX objects.)

IE 9版本的JScript具有完全重写的垃圾收集器,该垃圾收集器使用了非常不同的技术。我与它的设计师聊天了一些,但是我没有足够的技术知识来深入讨论它的特性。

The IE 9 version of JScript has a completely rewritten garbage collector that uses very different technology; I have chatted a bit with its designer but I do not have enough technical knowledge to discuss its characteristics in any kind of depth.

JScript .NET当然使用CLR垃圾收集器。

JScript .NET of course uses the CLR garbage collector.

请问您为什么要关心所有这一切?东东?

Can I ask why you care about all this stuff?

另外,请注意,我已经有十多年没有看过这段代码了;对所有这些都采取适当的怀疑态度。我的记忆可能有问题。

Also, note that I haven't looked at this code in over a decade; take all of this with the appropriate level of skepticism. My memory may be faulty.

这篇关于在VBScript中销毁对象的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆