我是否需要在每个“foreach”上释放 COM 对象?迭代?

发布于 2024-10-05 10:07:10 字数 1175 浏览 1 评论 0原文

这是(潜在的)问题:

我创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-10-12 10:07:10

您不应该对 COM 对象使用 foreach 语句,因为引用是在幕后进行的,您无法控制释放。我会切换到 for 循环并确保您 永远不要对 COM 对象使用两个点。

其外观如下:

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);
}

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);
}
够运 2024-10-12 10:07:10

另一种方法是创建您自己的迭代器函数:

IEnumerable<ComObject> GetChildItems(this ComObjectClass manager) {
    ComObject comObject = null;

    ComObject[] collectionOfComItems = manager.GetCollectionOfItems();
    for (int i = 0; i < collectionOfComItems.Length; i++) {
        try {
            comObject = collectionOfComItems[i];

            yield return comObject;
        } finally {
            if (comObject != null)
                Marshal.ReleaseComObject(comObject);
        }
    }

    yield break;
}

private static void doStuff() {
    ComObjectClass manager = null;

    try {
        manager = new ComObjectClass();

        foreach (ComObject item in manager.GetChildItems()) {
            Log.Debug(item.Name);
        }
    } finally {
        releaseComObject(manager);
    }
}

我觉得这使您的代码更具可读性,特别是当您需要多次迭代子项时。

Another way is to create your own iterator function :

IEnumerable<ComObject> GetChildItems(this ComObjectClass manager) {
    ComObject comObject = null;

    ComObject[] collectionOfComItems = manager.GetCollectionOfItems();
    for (int i = 0; i < collectionOfComItems.Length; i++) {
        try {
            comObject = collectionOfComItems[i];

            yield return comObject;
        } finally {
            if (comObject != null)
                Marshal.ReleaseComObject(comObject);
        }
    }

    yield break;
}

private static void doStuff() {
    ComObjectClass manager = null;

    try {
        manager = new ComObjectClass();

        foreach (ComObject item in manager.GetChildItems()) {
            Log.Debug(item.Name);
        }
    } finally {
        releaseComObject(manager);
    }
}

I feel this makes your code much more readable, especially if you need to iterate through the child items at multiple times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文