GCHandle to get address(pointer) of .net object

霸气de小男生 提交于 2019-11-28 00:18:31
VinayC

As Tim and thecoop has pointed out, GCHandle.Alloc may prevent garbage collection but actual object address can change as GC may move object around unless you pin the object. Further, your code is using GCHandleType.WeakTrackResurrection and that would not event prevent the garbage collection. GCHandle.ToIntPtr will give address of handle that can be round-tripped over unmanaged call. Actual object address will be given by AddrOfPinnedObject method.

Said all that, IMO, your code may serve the purpose of associating .NET object with unmanaged object. This is because GCHandle.To/FromIntPtr will get back you correct GCHandle and you can reach your .NET object via it (provided its not garbage collected). IMO, it should be immaterial if actual object address had changed or not.

You'll want to pin the GCHandle to stop the object moving around, as the GC moves objects around, so an unpinned pointer could become invalid. Pinning the object stops it moving:

GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();

You'll also have to free the handle when you're done:

handle.Free();

What you are getting is not actually an address.

As you notice, it seems to act like an address most of the time, and you can recall the object, by using GCHandle.FromIntPtr. However, the interesting issue is that you are using GCHandleType.WeakTrackResurrection.

If your weakly referenced object gets collected (it presumably can, since it is only weakly referenced by the GCHandle), you still have the IntPtr, and you can pass it to GCHandle.FromIntPtr(). If you do so, then you get back null, assuming the IntPtr has not been recycled.

(If the IntPtr has been recycled by the CLR for some reason, then you are in trouble. I'm not sure whether this can happen.)

You are better off using either GCHandleType.Normal, or GCHandleType.Pinned (if you need to take the address of the object in unmanaged code), if you want a strong reference to the object.

(To use GCHandleType.Pinned, your object must e.g. be primitive, or have [StructLayout] attribute, or be an array of such objects.)

AFAIK the address does not change because of allocing, is it true

The address of a managed object does change. The garbage collector is free to move objects in memory. When the garbage collector runs, it collects unused objects, then rearranges the remaining objects to minimize the overall size of the managed heap.

does anyone have a better idea to serve my purpose?

I'm not sure you'll find a good way to keep hold of a pointer for a managed object for long periods of time. It's possible to pin objects in memory, and get their address that way, but in C# it's possible to pin objects only within a single method.

It would help if you explained in more detail what you're going to do with the pointer once you have it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!