> Using Excel 9 / 2000, BTW.
Well, exactly. That's why they call it "non-deterministic". ;-)
Seriously, the randomness of garbage collection is the challenge with these
types of errors. Try varying the loop count, especially raising it quite a
bit. Clearly ReleaseComObject is failing under certain circumstances.
Forcing garbage collection just before exiting the function solves the
problem, both in this example and in my actual application. In my actual
application the problem is occurring inside a loop, so not a viable
workaround. Since you are using Excel 2003, perhaps there is a problem
specific to 2000. What I want to find out is whether my usage is incorrect
or if it is a bug. Your display name says "Microsoft", so perhaps you can
try it out on 2000? Any insight would be appreciated.
Just in passing I remark that after perusing this group and the tech
articles I'm rather astounded at what a low priority MS seems to have given
Office interop, as opposed to ADO, which they clearly spent a lot of time
thinking about. I would think Office would be at least as much at the core of
the .NET strategy. Even if the recommend usage worked all the time, it's
incredibly awkward to use. If I can write a decent wrapper in a day or two,
you'd think it would be easy for MS.
> maybe you have a timing issue, because when I try that code in my machine,
> no memory errors occur. I am using Excel 2003.
Chuck Heatherly - 28 Jan 2005 14:50 GMT
>Well, exactly. That's why they call it "non-deterministic". ;-)
>
>Seriously, the randomness of garbage collection is the challenge with these
>types of errors. Try varying the loop count, especially raising it quite a
>bit. Clearly ReleaseComObject is failing under certain circumstances.
I tried the code sample using Excel 2000 and also didn't see any memory errors,
running the application through the IDE and from the command line.
For what it's worth, the MSDN doc for ReleaseComObject says this:
<quote>
[Visual Basic]
Public Shared Function ReleaseComObject(ByVal o As Object) As Integer
Return Value
The new value of the reference count of the runtime callable wrapper associated
with o. This value is typically zero since the runtime callable wrapper keeps
just one reference to the wrapped COM object regardless of the number of managed
clients calling it.
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.
</quote>
I use ReleaseComObject with one of my programs that use ADODB objects, and when
I wrote a method like the note suggests, I noticed that the loop sometimes took
5 or 6 iterations per COM object. So see if this helps.
Chuck