无法删除变量,因为它已经过优化且不可删除 - 释放 COM 对象 [英] Cannot remove variable because it has been optimized and is not removable - releasing a COM object

查看:10
本文介绍了无法删除变量,因为它已经过优化且不可删除 - 释放 COM 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本结束时,我使用 'ie' |ForEach-Object {Remove-Variable $_ -Force}.它在 PS 2 (Windows 7) 中运行良好,但在 PS 5 (Windows 10) 中会引发错误:

At the end of my script I use 'ie' | ForEach-Object {Remove-Variable $_ -Force}. It works fine in PS 2 (Windows 7) but PS 5 (Windows 10) throws an error:

无法删除变量,即因为变量已被优化并且不可拆卸.尝试使用 Remove-Variable cmdlet(没有任何别名),或点源您用来删除变量.

Cannot remove variable ie because the variable has been optimized and is not removable. Try using the Remove-Variable cmdlet (without any aliases), or dot-sourcing the command that you are using to remove the variable.

我怎样才能让它在 PS 5 上玩得更好;还是我应该只使用 Remove-Variable 'ie' -Force?

How can I make it play nice with PS 5; or should I just use Remove-Variable 'ie' -Force?

推荐答案

删除 COM 对象的推荐方法是调用 ReleaseComObject 方法,将对象引用 ($ie) 传递给 COM 对象的实例.

The recommended way to remove COM objects is to call the ReleaseComObject method, passing the object reference ($ie) to the instance of your COM object.

这里是来自 Windows PowerShell Tip of the一周展示了如何摆脱 COM 对象:

Here is more detailed explanation and sample code from a Windows PowerShell Tip of the Week that shows how to get rid of COM objects:

每当您从公共语言运行时(即当您从Windows PowerShell),该 COM 对象被包装在运行时可调用wrapper,"并且引用计数增加;该引用计数帮助 CLR(公共语言运行时)跟踪哪个 COM对象正在运行,以及有多少 COM 对象正在运行.什么时候您从 Windows PowerShell 中启动 Excel,Excel 被打包在运行时可调用包装器中,并且引用计数递增到 1.

Whenever you call a COM object from the common language runtime (which happens to be the very thing you do when you call a COM object from Windows PowerShell), that COM object is wrapped in a "runtime callable wrapper," and a reference count is incremented; that reference count helps the CLR (common language runtime) keep track of which COM objects are running, as well as how many COM objects are running. When you start Excel from within Windows PowerShell, Excel gets packaged up in a runtime callable wrapper, and the reference count is incremented to 1.

这很好,除了一件事:当你调用 Quit 方法并且终止 Excel,CLR 的引用计数不会减少(也就是说,它不会被重置回 0).而且因为参考count 不为 0,CLR 保持对 COM 对象的持有:其他事情,这意味着我们的对象引用($x)仍然有效并且 Excel.exe 进程继续运行.这绝对是不是一件好事;毕竟,如果我们希望 Excel 继续运行,我们可能一开始就不会调用 Quit 方法....

That’s fine, except for one thing: when you call the Quit method and terminate Excel, the CLR’s reference count does not get decremented (that is, it doesn’t get reset back to 0). And because the reference count is not 0, the CLR maintains its hold on the COM object: among other things, that means that our object reference ($x) is still valid and that the Excel.exe process continues to run. And that’s definitely not a good thing; after all, if we wanted Excel to keep running we probably wouldn’t have called the Quit method in the first place. ...

... 调用 ReleaseComObject 方法 [with] 我们的Excel 的实例 ... 减少对象的引用计数问题.在这种情况下,这意味着它将更改参考将我们的 Excel 实例从 1 计数到 0.这是一件好事:一旦引用计数达到 0,CLR 就会释放它对对象和进程终止.(这次真的终止.)

... calling the ReleaseComObject method [with] our instance of Excel ... decrements the reference count for the object in question. In this case, that means it’s going to change the reference count for our instance of Excel from 1 to 0. And that is a good thing: once the reference count reaches 0 the CLR releases its hold on the object and the process terminates. (And this time it really does terminate.)

$x = New-Object -com Excel.Application
$x.Visible = $True
Start-Sleep 5
$x.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($x)
Remove-Variable x

消息无法删除变量,即因为变量已优化且不可删除."你得到了,很可能意味着你试图访问(检查、监视或以其他方式访问)一个已被优化器删除的变量.

The message "Cannot remove variable ie because the variable has been optimized and is not removable." you get, most likely means you have tried to access (inspect, watch, or otherwise access) a variable which has been already removed by the optimizer.

这篇关于无法删除变量,因为它已经过优化且不可删除 - 释放 COM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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