I am trying to delete C++ pointer from C# and I do not know what I am doing wrong. Simply application crashes when I am trying to delete the Pointer.
In C# I have
IntPtr jointAreaPointer = IntPtr.Zero;
that holds a reference from c++ pointer that I conver to double array
double[][] polylinePoints = FromNative(ref jointAreaPointer, numberOfPairs, jointAreaCountPointerFlatArray);
public static double[][] FromNative(ref IntPtr data, int m, int[] n)
{
var matrix = new double[m][];
for (int i = 0; i < m; i++)
{
int c = n[i] * 3;
matrix[i] = new double[c];
Marshal.Copy(Marshal.ReadIntPtr(data), matrix[i], 0, c);
data = (IntPtr)(data.ToInt64() + IntPtr.Size);
}
return matrix;
}
I get the values from the array.
Then I try to delete the nested array pointer:
ReleaseNestedDouble(jointAreaPointer, numberOfPairs);
[DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void ReleaseNestedDouble(IntPtr arr,int count, bool isArray = true); // release input coordinates
That looks like this in c++ and crashes immediately when looping:
PINVOKE void ReleaseNestedDouble(double** arr, int count, bool isArray) {
for (int i = 0; i < count; i++) {
delete[](arr[i]);
}
delete[](arr);
}