1

I'm quite new to C# and I'm having trouble releasing unmanaged resource. For the function CharPtrToString, is it necessary to release IntPtr? In addition, would it be safe to call List < MyStruct >.clear() without causing a memory leak?

    public string CharPtrToString(MycharArray chararray)
    {
        IntPtr ipp = (IntPtr)chararray;
        string s = Marshal.PtrToStringAnsi(ipp)
        //need to free Ipp?
        return s;
    }

    public struct MyStruct
    {
          public Int int1;
          public MyCharArray charArray;
    }

    public unsafe struct MyCharArray
    {
          public char* charPointer;
    }
ntmt
  • 163
  • 1
  • 1
  • 11
  • 1
    Not an answer, but taking the _"I'm quite new to C#"_ and the `unsafe... char*` together - are you doing this just to satisfy some curiosity, or is there an underlying requirement at play here? There may be a more idiomatic way of achieving what you're after. – James Thorpe May 08 '18 at 21:09
  • `IntPtr ipp = (IntPtr)chararray; ` do you mean `IntPtr ipp = (IntPtr)chararray.charPointer;` ? – user287107 May 08 '18 at 21:10
  • I am assuming you are coming from c++ world. But as a rule of thumb for c# : Avoid unsafe type whenever possible! Let the garbage collector do its job. If you can't afford the little performance penalty then c++ is probably still your best friend. – Steve May 08 '18 at 21:13
  • by the way, why are you not using a char[] array, and constructing a string over the constructor ? – user287107 May 08 '18 at 21:15
  • @JamesThorpe I'm currently using HDF5DotNet library to read data into the specified structure. One field of the data contains a variable length string, so that's why I have to use char* to convert the data into c# string. The reading is fine and conversion is fine, but i'm uncertain on how to clean that structure since I don't have a use for it anymore. – ntmt May 08 '18 at 21:21
  • @ntmt using pointers for this is _very_ unlikely for .Net. please open a new question, and post the complete code containing the hdf functions. I'm very sure that there is a solution using variable length `char[]` arrays – user287107 May 08 '18 at 22:51
  • @user287107 I opened a new question [here](https://stackoverflow.com/questions/50243898/proper-disposal-of-unamanged-struct) thank you for your help. I changed MyStruct to hold intptr instead instead of a char* for brevity. – ntmt May 09 '18 at 00:29

1 Answers1

0

if possible, when assigning the charPointer variable, you should try to use the "fixed" keyword. this fixes the pointer so the garbage collection does not clean up the pointer. Then you also do not need to release it, it will be automatically released after the fixed block. This depends how your other code is.

https://learn.microsoft.com/en-US/dotnet/csharp/language-reference/keywords/fixed-statement

so it would look about like this

 fixed(char* charPointer = ... )
 {
    IntPtr ipp = (IntPtr)charPointer;
    string s = Marshal.PtrToStringAnsi(ipp)
 }
user287107
  • 9,286
  • 1
  • 31
  • 47
  • if the `charPointer` is unmanaged or pinned somewhere else, it may not need to be `fixed` *at all*; context matters – Marc Gravell May 08 '18 at 21:16
  • My code follows: a HelperClass that loads the data, given a file directory, into the structure (int and intptr) and return a list of string (result of the multiple intptr). The HelperClass has no members just methods. – ntmt May 08 '18 at 21:45