dan gallery
Posts: 83
Joined: 2009-10-19
|
Hi guys, please consider the following code. Should getCellText(Excel.Range rng) perform a Marshal.Release of the com object variable that was passed in? Or should only the calling program do the Marshal.release?
Thanks,
Dan
public void parentCode() {
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook aWorkbook = excelApp .Application.Workbooks.Open("someFile.xls"...);
Excel.Range rng = (Excel.Range) aWorkbook.ActiveSheet.get_Range("A1", "A1");
string cellText = getCellText(rng);
if (excelApp != null) Marshal.ReleaseComObject(excelApp);
if (aWorkbook != null) Marshal.ReleaseComObject(aWorkbook );
if (rng!= null) Marshal.ReleaseComObject(rng);
}
private string getCellText(Excel.Range rng) {
string cellText = "";
try {
cellText = rng.Value.ToString();
}
catch (Exception){}
// should I do this???
if (rng!= null) Marshal.ReleaseComObject(rng);
return cellText;
} [CODE] |
|
Andrei Smolin
Add-in Express team
Posts: 18821
Joined: 2006-05-11
|
Hi Dan,
You should decide for yourself which of the procedures you prefer to release that COM object. I always use the approach "the caller is responsible for releasing a COM object that was passed in", in other words I'd comment out that line in getCellText.
Note that the parentCode method creates and leaves unreleased two COM objects:
- excelApp.Workbooks
- aWorkbook.ActiveSheet
Andrei Smolin
Add-in Express Team Leader |
|
dan gallery
Posts: 83
Joined: 2009-10-19
|
Thanks Andrei, that's what I thought. And yes, right you are about the 2 unreleased variables.
Dan |
|