title: Dll interop and memory management concerning arrays
body: <p>My question is about memory usage when interoping dlls in .Net (I am using C#, but the answer should apply to other .Net languages).</p>
  
  <p>Say I have a C function in a dll that takes an array:</p>
  
  <pre><code>extern __declspec(dllexport) void Func(int * arr, int len);
  </code></pre>
  
  <p>I have a choice of two corresponding calls in C#:</p>
  
  <pre><code>[DllImport("Lib.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern void Func([In] IntPtr arr, [In] int len);
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>[DllImport("Lib.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern void Func([In] int[] arr, [In] int len);
  </code></pre>
  
  <p>If the C function does not deallocate the array, will the latter interop method cause a memory leak by assuming the C function will free the marshalled array or will the interop system know to free the marshalled array?</p>