As far as I'm aware, in COM Interop if we cross the .NET/COM boundary we get an increment in the RCW internal counter. So I created a VSTO Excel workbook (2013) app and ran the code:
private void RCWWorkbooks()
{
Excel.Workbooks wbs = Application.Workbooks;Excel.Workbook book1 = wbs[1];
Excel.Workbook book2 = wbs[1];
Excel.Workbook book3 = wbs[1];
Debug.WriteLine("Book3:= " + Marshal.ReleaseComObject(book3));
Debug.WriteLine("Book2:= " + Marshal.ReleaseComObject(book2));
Debug.WriteLine("Book1:= " + Marshal.ReleaseComObject(book1));
}
And the output is as I expected:
Book3:=2
Book2:=1
Book1:=0
i.e. we have 3 references, a total count of 3 within the RCW which each get decremented by 1 when I invoke the ReleaseCOMObject
I did the same for a worksheet test: This time I get the results:
private void RCWSheets()
{
Excel.Sheets wks = Application.Workbooks[1].Worksheets;
Excel.Worksheet sht1 = wks[1];
Excel.Worksheet sht2 = wks[1];
Excel.Worksheet sht3 = wks[1];
Debug.WriteLine("Sheet3:= " + Marshal.ReleaseComObject(sht3));
Debug.WriteLine("Sheet2:= " + Marshal.ReleaseComObject(sht2));
Debug.WriteLine("Sheet1:= " + Marshal.ReleaseComObject(sht1));
}
And the output wasn't exactly as I expected.
Sheet3:=3
Sheet2:=2
Sheet1:=1
I can't work out why sheet3:=3. I was expecting this to be 2.
Next I tried a range test with the following code:
private void RCWRanges()
{
Excel.Worksheet sht = Application.Workbooks[1].Worksheets[1];
Excel.Range r1 = sht.Range["A1"];
Excel.Range r2 = sht.Range["A1"];
Excel.Range r3 = sht.Range["A1"];
Debug.WriteLine("Range3:= " + Marshal.ReleaseComObject(r3));
Debug.WriteLine("Range2:= " + Marshal.ReleaseComObject(r2));
Debug.WriteLine("Range1:= " + Marshal.ReleaseComObject(r1));
}
Again, the output wasn't as I expected:
Range3:=0
Range2:=0
Range1:=0
So my questions are:
- Why did the sheet test return an extra count. It returned 3 where I was expecting two.
- Why did the range test return 0 for all the reference counts? This suggests to me that the range request doesn't cross the .NET/COM barrier.
Thanks