1. <tfoot id='m5CHB'></tfoot>

      • <bdo id='m5CHB'></bdo><ul id='m5CHB'></ul>

      <legend id='m5CHB'><style id='m5CHB'><dir id='m5CHB'><q id='m5CHB'></q></dir></style></legend>
        <i id='m5CHB'><tr id='m5CHB'><dt id='m5CHB'><q id='m5CHB'><span id='m5CHB'><b id='m5CHB'><form id='m5CHB'><ins id='m5CHB'></ins><ul id='m5CHB'></ul><sub id='m5CHB'></sub></form><legend id='m5CHB'></legend><bdo id='m5CHB'><pre id='m5CHB'><center id='m5CHB'></center></pre></bdo></b><th id='m5CHB'></th></span></q></dt></tr></i><div id='m5CHB'><tfoot id='m5CHB'></tfoot><dl id='m5CHB'><fieldset id='m5CHB'></fieldset></dl></div>

        <small id='m5CHB'></small><noframes id='m5CHB'>

      1. 我是否需要在每次“foreach"迭代时释放 COM 对象?

        Do I need to release the COM object on every #39;foreach#39; iteration?(我是否需要在每次“foreach迭代时释放 COM 对象?)
        <i id='7FYYE'><tr id='7FYYE'><dt id='7FYYE'><q id='7FYYE'><span id='7FYYE'><b id='7FYYE'><form id='7FYYE'><ins id='7FYYE'></ins><ul id='7FYYE'></ul><sub id='7FYYE'></sub></form><legend id='7FYYE'></legend><bdo id='7FYYE'><pre id='7FYYE'><center id='7FYYE'></center></pre></bdo></b><th id='7FYYE'></th></span></q></dt></tr></i><div id='7FYYE'><tfoot id='7FYYE'></tfoot><dl id='7FYYE'><fieldset id='7FYYE'></fieldset></dl></div>

        <legend id='7FYYE'><style id='7FYYE'><dir id='7FYYE'><q id='7FYYE'></q></dir></style></legend>
          <bdo id='7FYYE'></bdo><ul id='7FYYE'></ul>

        • <small id='7FYYE'></small><noframes id='7FYYE'>

          <tfoot id='7FYYE'></tfoot>

                <tbody id='7FYYE'></tbody>

                  本文介绍了我是否需要在每次“foreach"迭代时释放 COM 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是(潜在的)问题:

                  我创建了一个 COM 对象,然后使用foreach"遍历它返回的集合中的每个元素.我是否需要释放我在集合中遍历的每个单独元素?(见下面的代码.)如果是这样,我想不出一种方法可以有效地从finally"语句中释放它,以防在操作项目时出现错误.

                  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.

                  有什么建议吗?

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

                  推荐答案

                  您不应该将 foreach 语句与 COM 对象一起使用,因为在您无法控制的幕后进行了引用过度释放.我会切换到 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 a for loop and make sure you never use two dots with COM objects.

                  看起来会是这样:

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

                  这篇关于我是否需要在每次“foreach"迭代时释放 COM 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                      <small id='2LgXF'></small><noframes id='2LgXF'>

                          <bdo id='2LgXF'></bdo><ul id='2LgXF'></ul>

                          1. <i id='2LgXF'><tr id='2LgXF'><dt id='2LgXF'><q id='2LgXF'><span id='2LgXF'><b id='2LgXF'><form id='2LgXF'><ins id='2LgXF'></ins><ul id='2LgXF'></ul><sub id='2LgXF'></sub></form><legend id='2LgXF'></legend><bdo id='2LgXF'><pre id='2LgXF'><center id='2LgXF'></center></pre></bdo></b><th id='2LgXF'></th></span></q></dt></tr></i><div id='2LgXF'><tfoot id='2LgXF'></tfoot><dl id='2LgXF'><fieldset id='2LgXF'></fieldset></dl></div>
                            <tfoot id='2LgXF'></tfoot>

                              <tbody id='2LgXF'></tbody>
                            <legend id='2LgXF'><style id='2LgXF'><dir id='2LgXF'><q id='2LgXF'></q></dir></style></legend>