Keith Hill
Nov 12, 12:55 pm show options
Newsgroups: microsoft.public.windows.server.scripting
From: "Keith Hill"- Find messages by this author
Date: Sat, 12 Nov 2005 10:55:39 -0700
Local: Sat, Nov 12 2005 12:55 pm
Subject: Re: [MSH] How to dispose a COM object
Reply | Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse
"Jouko Kynsijärvi"wrote in message
news:%23pf9pW65FHA.2040@TK2MSFTNGP14.phx.gbl...
> You can release the COM object by using Marshal.ReleaseComObject():
Keep in mind that if you marshal a COM object back and forth between
different COM servers and MSH, the ref count on the RCW can get bumped up
past 1. That means that if you really, really want to get rid of the COM
object you typically do this:
while
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel)
-gt 0) {}
Essentially ReleaseComObject returns the new ref count and you want to
continue calling it until the ref count reaches 0. At least this was the
way it worked in .NET 1.1. And indeed the .NET 2.0 docs say:
Note
To ensure that the runtime callable wrapper and the original COM
object are released, construct a loop from which you call this method until
the returned reference count reaches zero.
Yeah I've been bit by this issue before.
--
Keith
Experimenting with a different format of blogs...