While researching a problem someone was having in the newsgroups this weekend, I ran into section 1.1.4.2 of the CIL spec (Partition III), which covers managed pointers. While I understand what this section talks about, some questions did come up which I'm not sure about:
- What would managed pointers pointing to unmanaged memory be useful for? Obviously, the GC can't do anything with the memory they point to, anyway.
- Regarding the managed-to-unmanaged pointer conversion safety, I'm not sure I understand what they mean what they say that the conversion is safe if one of the mentioned rules hold true.... but what do they mean by "safe"? Would such an image be deemed verifiable? I don't think so (heck, if rule 1 holds true, and the pointer points to unmanaged memory, what's safe about that?)
3 Comments
archived from WordPress — closedSay you have a method that takes a reference to an int as argument (a managed pointer), the fact that you can pass an unmanaged pointer to an int can be convenient.
By "safe" they mean that the GC will not be confused. If the managed pointer points to unmanaged data, it is perfectly fine to convert it to an unmanaged pointer (i.e. it is "safe"), however when the managed pointer points to managed data, you shouldn't convert the pointer to an unmanaged pointer (but you could, although that would be "unsafe", i.e. the system would probably crash at some later point in time).
So, to put it another way: The conversion is safe. Using the resulting pointer probably isn't :)
No, that's not what they mean. In this context, "safe" means a valid conversion so that the pointer still points to what it's supposed to ie the thing it used to point to cannot have been moved/freed by the GC.
In the wider sense it's far from safe. Give me one carefully chosen managed pointer and a little knowledge, and if I can convert it to unmanaged I can take over the CLR!!!
Lemme see...start with a pointer to a value type as the only field in an object; back up to the object header; find the uplink; ... (hee hee, this is fun!)