0

Hello this question is a continuation of this question. Provided below is a short version of the code that is loading the data into the structure. The HDF5DotNet library is found here using the 32 bit version with vs2015. My question is, will MyStruct leak memory when SomeCallerClass call HDF5GetSubGroups?

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
     }

}
ntmt
  • 163
  • 1
  • 1
  • 11
  • I read your other question and still not sure why you need `MyStruct`. You can just use a `string` or `char[]`. – CodingYoshi May 09 '18 at 01:57
  • @CodingYoshi According to the HDF5DotNet library [here](http://hdf5.net/FAQ.aspx) the only way I can read variable length strings in the HDF5 file was to use intptr and use Interop.Marshal to convert them to .NET strings. I believe I've tried string and char[] before, but it didn't return me the proper string that was written to the h5 file. – ntmt May 09 '18 at 02:05
  • Ok I see. Please see [this](https://stackoverflow.com/questions/6093449/proper-intptr-use-in-c-sharp). – CodingYoshi May 09 '18 at 02:15
  • can you please try to close the file before reading out the pointer? if you put a `H5A.close` directly after the `H5A.read` call, the marshall function should return an error or some weired string. if this is the case you dont neet to free the string. – user287107 May 12 '18 at 12:35

0 Answers0