I'm pinning and unpinning with:
GCHandle pinArray(object a)
return GCHandle.Alloc(a, GCHandleType.Pinned);
void unpinArray(GCHandle h)
// how to?
// unmanaged operations
compute(array,...); // only passing with "array" for simplicity everywhere
You don't need to. The
GCHandle relates to managed memory; unmanaged memory does not require any kind of
GCHandle. Nor is it possible to talk to that memory as though it is a managed array. Instead, you need to accept that the unmanaged data is a pointer and only a pointer. You can abstract over the top of that to hide these details, but it doesn't change the reality. Fortunately, to the casual observer, talking to a
SomeType is very similar to talking to a
SomeType* - as long as you pass the lengths around yourself.
In the future, the upcoming
Span<T> does a great job of unifying pointers and arrays, but that is only experimental at the moment.