我是否需要在每个“foreach”上释放 COM 对象?迭代?
这是(潜在的)问题:
我创建一个 COM 对象,然后使用“foreach”迭代它返回的集合中的每个元素。我是否需要释放集合中迭代的每个单独元素? (参见下面的代码。)如果是这样,我想不出一种方法可以有效地从“finally”语句中释放它,以防在操作该项目时出现错误。
有什么建议吗?
private static void doStuff()
{
ComObjectClass manager = null;
try
{
manager = new ComObjectClass();
foreach (ComObject item in manager.GetCollectionOfItems())
{
Log.Debug(item.Name);
releaseComObject(item); // <-- Do I need this line?
// It isn't in a 'finally' block...
// ...Possible memory leak?
}
}
catch (Exception) { }
finally
{
releaseComObject(manager);
}
}
private static void releaseComObject(object instance)
{
if (instance != null)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(instance);
}
catch
{
/* log potential memory leak */
Log.Debug("Potential memory leak: Unable to release COM object.");
}
finally
{
instance = null;
}
}
}
Here's the (potential) problem:
I create a COM object, and then use a 'foreach' to iterate through each element in a collection it returns. Do I need to release each individual element I iterate through in the collection? (See code below.) If so, I can't think of a way to effectively to release it from within a 'finally' statement, just in case there is an error as the item is being operated upon.
Any suggestions?
private static void doStuff()
{
ComObjectClass manager = null;
try
{
manager = new ComObjectClass();
foreach (ComObject item in manager.GetCollectionOfItems())
{
Log.Debug(item.Name);
releaseComObject(item); // <-- Do I need this line?
// It isn't in a 'finally' block...
// ...Possible memory leak?
}
}
catch (Exception) { }
finally
{
releaseComObject(manager);
}
}
private static void releaseComObject(object instance)
{
if (instance != null)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(instance);
}
catch
{
/* log potential memory leak */
Log.Debug("Potential memory leak: Unable to release COM object.");
}
finally
{
instance = null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该对 COM 对象使用
foreach
语句,因为引用是在幕后进行的,您无法控制释放。我会切换到for
循环并确保您 永远不要对 COM 对象使用两个点。其外观如下:
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 afor
loop and make sure you never use two dots with COM objects.The way this would look would be:
另一种方法是创建您自己的迭代器函数:
我觉得这使您的代码更具可读性,特别是当您需要多次迭代子项时。
Another way is to create your own iterator function :
I feel this makes your code much more readable, especially if you need to iterate through the child items at multiple times.