GCHandle to get address(pointer) of .net object

后端 未结 4 754
忘了有多久
忘了有多久 2020-12-06 13:29

I managed to get the address of a .net object by

GCHandle objHandle = GCHandle.Alloc(obj,GCHandleType.WeakTrackResurrection);
int address = GCHandle.ToIntPtr         


        
相关标签:
4条回答
  • 2020-12-06 14:10

    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.)

    0 讨论(0)
  • 2020-12-06 14:14

    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();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-06 14:28

    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.

    0 讨论(0)
提交回复
热议问题