A pointer is passed to me from a C++ dll as followed:
[DllImport("myfile.dll", EntryPoint = "LoadFile", SetLastError = true, CharSet = CharSet.None)]
public static extern IntPtr dLoadFile(string x);
IntPtr p = dLoadFile("myfile");
//Do things with p.
Marshal.FreeHGlobal(p) //crash on this line with exception below.
System.Runtime.InteropServices.COMException: 'One or more arguments are invalid (Exception from HRESULT: 0x80000003)'
Should I free the memory allocated to IntPtr ? And if yes, how to do it properly ? Obviously it seems that Marshal.FreeHGlobal() is not the way to go...
Thanks