The native heap/managed heap barrier

Garrett asks:

You mentioned:

class A{}; array^ arr = gcnew array(10);.

Are you saying that whidbey will support this? What is it doing to the native pointer? Boxing?

Managed array of native pointers to native objects? This didn't work in Everett, but it will in Whidbey. It probably wasn't a hard fix. Let's look a the simple case, having a native pointer inside of a managed object on the GC heap. Take the following example:

class N{}; //some native class
ref struct R{
N* pn; //native (__nogc) pointer to native class
R(){ pn = new N; }
};

But can't the GC heap move objects around at will? Yes, it can. This was puzzling to me for a while, also. Look closely at the constructor for R. We are calling new on the native object. Not gcnew, just regular new. So, the native object is going on the native heap. The pointer to that object might be inside R, on the managed heap, but the location the pointer is referencing won't move. Imagine it like a rubber band, one end tacked down in the native heap, the other in the GC heap. The native side holds still, doesn't move. The managed side might be relocated, but the pointer still points to the same position in the native heap.

Comments

  • Anonymous
    December 23, 2003
    The comment has been removed
  • Anonymous
    December 23, 2003
    Hmm... actually, I ildasm'ed the results, and I don't think I like what its doing. Let me get back to you on that one.