What you receive is not really an address.
As you noticed, it works as the address most often, and you can remember the object using GCHandle.FromIntPtr. However, an interesting problem is that you are using GCHandleType.WeakTrackResurrection .
If your object with low reference information is collected (maybe it can, because it weakly refers to GCHandle), you still have IntPtr, and you can pass it to GCHandle.FromIntPtr () . If you do, you will return to zero considering that IntPtr has not been redesigned.
(If for some reason IntPtr has been redesigned by CLR, then you have problems. I'm not sure if this could 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 be, for example, primitive or have the [StructLayout] attribute or be an array of such objects.)
Tim Lovell-Smith
source share