Do I need to release the COM object on every 'foreach' iteration?
You should not use a foreach
statement with a COM object, as a reference is made behind the scenes to which you have no control over releasing. I would switch to a for
loop and make sure you never use two dots with COM objects.
The way this would look would be:
try
{
manager = new ComObjectClass();
ComObject comObject = null;
ComObject[] collectionOfComItems = manager.GetCollectionOfItems();
try
{
for(int i = 0; i < collectionOfComItems.Count; i++)
{
comObject = collectionOfComItems[i];
ReleaseComObject(comObject);
}
}
finally
{
ReleaseComObject(comObject);
}
}
finally
{
ReleaseComObject(manager);
}