What happens when I allocate memory (structs within double pointer) in C++ and use it via Marshall with C#?
Do I have to clean up the memory or does the C# GarbageCollection do it for me? Marshall.FreeHGlobal(vsResult);
doesn't work.
[StructLayout(LayoutKind.Sequential)]
public struct MyCppResults
{
[MarshalAs(UnmanagedType.I4)] public int ResultSize;
public unsafe double* Result;
}
[DllImport("SomeVeryFastAlgorithm.dll")]
public static extern double[] LoadResults()
{
var resultsPtr = GetResults();
var vsResult = Marshal.PtrToStructure<MyCppResults>(resultsPtr);
var resultMatrix = new double[vsResult.ResultSize];
unsafe
{
for (var i = 0; i < resultMatrix.Length; i++)
resultMatrix[i] = vsResult.Result[i];
}
return resultMatrix;
}