Marshal.ReleaseComObject(Range) runtime error

T

taxi123

Summary====================
Without ReleaseComObject My AddIn Runs out of memory the second time it's
used.
With ReleaseComObject I get a runtime error stating I'm not passing in a
valid Com Object.

Details======================
Due to looping through many rows of data and using the Excel.Range object to
extract data, I make use of a range object over 20,000 times during the
execution of my AddIn. It's my understanding that this ties up a lot of COM
references that are not freed up when my AddIn is complete.

If I run my AddIn a second time, I get an out of Memory error at some random
place in my code. This has led me to the recommendations of others to start
using FinalReleaseComObject or ReleaseComObject but I can't get
either to work because the arguments I'm using to call them don't seem to be
valid.

Using:
VSTO 3.0
Visual Studio 2008
Office 2007 Excel

using System;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.Office.Tools.Ribbon;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel.Extensions;

//This is the function that I call many times during my AddIn.
//It stops working when I add the ReleaseComObject Line
private StringBuilder GetRowCellData(Excel.Worksheet sheet, int row, int
column)
{
Excel.Range cellRange = (Excel.Range)sheet.Cells[row, column];
StringBuilder returnString = new StringBuilder();
returnString.Append(cellRange.get_Value(m_missing));
Marshal.ReleaseComObject(cellRange); //This line has Runtime Error
"ArgumentException was unhandled by user code"
return returnString;
}

==========================
Runtime Error Using ReleaseComObject:
==========================
The object's type must be __ComObject or derived from __ComObject.
Parameter name: o

My range object (and all my other Excel objects) appear to be of the
following type:
{System.Runtime.Remoting.Proxies.__TransparentProxy}


========================
Runtime Error Without Using ReleaseComObject(On Second Run of AddIn):
========================
Not enough storage is available to complete this operation. (Exception from
HRESULT: 0x8007000E (E_OUTOFMEMORY))

Any help is greatly appreciated.

Thanks...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top