I was wondering if there is any way a native pointer can point to a Managed Heap ? I know this would be a dangerous thing to do, given heap compaction and GC cycles changing object address but is it still possible ? An example would be appreciated.
Also, is it true that a Handle (^) cannot point to native heap at all ?
Thanks.
Yes, that’s supported but the object that’s pointed-to needs to be pinned. You do so with .NET’s GCHandle::Alloc() or by using the C++/CLI
pin_ptr<>class. Pinning objects for a long time is Bad, they give the garbage collector a hard time since it needs to work around the rock in the road. Pinning is very common when calling native code.A tracking reference (hat) to a block in the native heap makes no sense, they only track objects in the garbage collected heap.