I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies from 2.0 for now), and I was trying to optimize some parts of the code. As it deals a lot with runtime callable wrappers, which need to be released, I ended up to create a utility method which loops until all references are released. The signature of the method is:
void ReleaseObject(object comObject)
After releasing all comObjects, I call GC.Collect and GC.WaitForPendingFinalizers (don't ask - anybody dealing with Office interop knows).
And ... as usual, I hit a corner case - if I do not assign the corresponding managed reference to null before the GC.Collect call, it does not cleanup properly.
So, my code looks like:
ReleaseObject(myComObject);
myComObject = null;
GC.Collect()
...
As, there are a bunch of xxx=null, I decided to put this in the util method, but as there is a difference between passing by reference, and passing a reference parameter, obviously I had to change the method to:
void ReleaseObject(out object comObject)
{
//do release
comObject = null;
}
and edit the caller to:
MyComClass myComObject = xxxx;
ReleaseObject(out myComObject);
This fails with a message: "Cannot convert from 'out MyComClass' to 'out object'"
While I can think of why it can be a problem (i.e. the reverse cast from object to MyComClass is not implicit, and there is no guarantee what the method will do), I was wondering if there is a workaround, or I need to stay with my hundreds assignments of nulls.
Note: I have a bunch of different COM objects types, thats why I need a "object" parameter, and not a type safe one.