正确处置未管理的结构

时间:2018-05-09 00:27:30

标签: c# unmanaged

你好这个问题是this question的延续。下面提供了将数据加载到结构中的简短版本的代码。使用带有vs2015的32位版本找到HDF5DotNet库here。我的问题是,当SomeCallerClass调用HDF5GetSubGroups时,MyStruct是否会泄漏内存?

using HDF5DotNet;
namespace CommonClass
{
        public class HDF5FileReader
        {
             public static List<string> HDF5GetSubGroups(string filePath)
             {
                  var returnList = new List<string>();
                  //... some HDF5 instantiating -- commented out for brevity
                  var myStruct = new MyStruct[numberOfThingsToRead];
                  H5A.read(someAttribute, someAttributeType, new H5Array<MyStruct>(myStruct));
                  string myStructVariableString = IntPtrToString(myStruct[0].intPtr);
                  returnList.Add(myStructVariableString);
                  //... closing some HDF5 instantiating -- commented out for brevity
                  return returnList
             }

         private string IntPtrToString(IntPtr ipp)
         {
              string s = Marshal.PtrToStringAnsi(ipp)
              //need to free ipp?
              return s;
         }
     }

     public struct MyStruct
     {
          public Int int1;
          public IntPtr intPtr;
     }
}

namespace CallerClass
{
     public class SomeCallerClass()
     {
         string someFilePath = "Path\To\HDF5File.h5"
         var stringList = HDF5FileReader.HDF5GetSubGroups(someFilePath)
         //Do Something with StringList -- add to observablecollection
     }

     public class UnloadingSomeCallerClass()
     {
          //Clear the observablecollection
     }

}

0 个答案:

没有答案