I have an external method that takes some parameters, allocates memory and returns a pointer.
[DllImport("some.dll", CallingConvention = CvInvoke.CvCallingConvention)] public static extern IntPtr cvCreateHeader( Size size, int a, int b);
I am well aware that the wrong practice is to allocate unmanaged memory in a managed application, but in this case I have no choice, since the dll is third-party.
There is an equivalent function that frees memory, and I know what size the allocated array is.
- How to bind the returned pointer so that the GC does not move it (without going unsafe)? "fixed" will not do this because this pointer is widely used throughout the class?
- Is there a better methodology for this p / Invoke?
Gilad source share