Lets say that I have one component which is doing something with Workbook object and somewhere in the middle of that method body I have call to some method of another class. For example:
public class MainComponent
{
public void MyMainMethod()
{
OtherComponent otherComponent = new OtherComponent();
Workbook document;
// some work with workbook object
// working with document and worksheet objects.
otherComponent.MethodCall(document);
// some work with workbook object and it's worksheets.
foreach(Worksheet sheet in document.Workheets)
// do something with sheet
}
}
public class OtherComponent
{
public void MethodCall(Workbook document)
{
string worksheetNames = "";
foreach(Worksheet sheet in document.Worksheets)
worksheetNames += sheet.Name;
Console.WriteLine(worksheetNames);
}
}
And in that otherComponent.MethodCall(document); I'm using document and I'm iterating through it's worksheets.
EDIT TO be more concrete on question. Should I call ReleaseCOMObject on document and on Worksheets in otherComponent.MethodCall(document) or not?
I never really had any good explanation on how should I manage this unmanaged code. I would really appreciate if someone could explain this to me.
You'll have to release all local objects manually in the scope where you create them. When using Office applications through Automation, don't rely on the garbage collector to clean up these objects - even if it gets it right, it may take time for it to kick in and you may end up with temporary objects holding references to other objects that you think are already gone.
This is a somewhat related question with more details that may apply to you if you try to run Excel from your application with Excel being hidden.
The part that's definitely relevant to you is this:
try..catch
block to capture any possible exception.Marshal.ReleaseComObject()
and then setting your variables tonull
as soon as you don't need them. Always release these objects in afinally
block to make sure that a failed Excel method call won't result in a dangling COM object.GC.Collect()
andGC.WaitForPendingFinalizers()
right after calling theApplication.Quit()
method to make sure that the .NET Framework releases all Excel COM objects immediately.Edit: this is after you added more details to your question.
In
otherComponent
you don't need to release theWorkbook
andDocument
objects. These two objects are created in your first object which implies that the first object is the owner. Since it's the first object that owns your top-level Excel objects (assuming you also have anApplication
object somewhere), your first object can callotherComponent
, pass inWorkbook
andDocument
and then on return, clean them up. If you never use any of these objects in yourMainComponent
, then you should create the Excel-related objects insideotherComponent
and clean them up there.With COM interop, you should create your COM objects as close to the place where you need them and release them explicitly as soon as you can. This is especially true for Office applications.
I made this class to make using COM objects easier: this wrapper is disposable, so you can use
using(...)
with your COM objects - when theusing
scope is over, the wrapper releases the COM object.