I'm not super handy at managing COM Objects. I've done a fair bit of reading, but I can't quite wrap my head around what happens when you have two references pointed at a single COM Object, and you release one of them. Consider the following [semi-]hypothetical:
I've created a basic wrapper class for Excel Workbooks in C#:
using ExcelInterop = Microsoft.Office.Interop.Excel;
...
public class WorkbookWrapper : IDisposable
{
protected internal ExcelInterop.Workbook _wb;
public WorkbookWrapper(ExcelInterop.Workbook workbook)
{
_wb = workbook;
}
public WorkbookWrapper(WorkbookWrapper workbookWrapper)
{
_wb = workbookWrapper._wb;
}
#region IDisposable
// basic Dispose function which releases _wb COM Object (omitted to shorten post length)
#endregion
...
}
For one particular project I'm working on, I've found it necessary to attach some additional information to WorkbookWrapper
objects, so I've extended it:
class LinkingWorkbookWrapper : WorkbookWrapper
{
internal string workbookGUID = null;
internal LinkingWorkbookWrapper(WorkbookWrapper workbookWrapper, string GUID) : base(workbook)
{
workbookGUID = GUID;
}
...
}
This particular class uses the WorkbookWrapper(WorkbookWrapper)
constructor to store a new reference to the Interop Workbook, this is suddenly problematic when using functions like the following:
public static void ProcessAllCharts(WorkbookWrapper wbw)
{
LinkingWorkbookWrapper lwbw = null;
if(wbw is LinkingWorkbookWrapper)
lwbw = (LinkingWorkbookWrapper)wbw;
else
lwbw = new LinkingWorkbookWrapper(wbw, GenerateGUID());
//... do stuff ...
}
For now let's say that the function must accept normal WorkbookWrapper
s. When is the appropriate time to dispose of lwbw
? If I do so at the end of ProcessAllCharts
I may separate the COM Object referenced in the original wbw
from its RCW; on the other hand, if I do not dispose of lwbw
, I run the risk of leaving a reference to the COM Object floating in the ether.