Why would a managed pointer on GC heap crash the JIT compiler?

Viewed 143

I've been reading the docs for the new C# 7.2 update, and I'm not sure I got the meaning of a specific part of the section about the ref struct declaration. Here it is:

[...] In addition, putting a managed pointer on the GC heap typically crashes at JIT time.

What does that mean exactly? Let's say I have something like this:

IntPtr ptr = Marshal.AllocHGlobal(sizeof(float) * 100);
float* pt = (float*)ptr.ToPointer();
Parallel.For(0, 10, i => 
{ 
    for (int j = 0; j < 10; j++) pt[i * 10 + j] = i * 10 + j;
});

Or another example:

float[,] m = new float[10, 10];
fixed (float* pt = m) // I know I could put this inside the lambda, but this
{                     // way the pointer should be captured by the closure
                      // and therefore end up on the GC heap
    Parallel.For(0, 10, i => 
    { 
        for (int j = 0; j < 10; j++) pt[i * 10 + j] = i * 10 + j;
    });
}

I'm assuming in both these cases that float* pointer would be captured and be stored on the GC heap, inside the closure, so:

  • Would either (or both) of these cases cause a problem?
  • Is this what that paragraph was talking about, or was that a different situation?
  • If this is the case, why would something like this cause the JIT compiler to crash?

Thanks!

0 Answers
Related