reference to handle / handle to reference

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i'm looking for a possibility in C# to get a unique Int32 handle for an
object reference. This handle must later (within the same process of course)
be convertible back to an object reference (call it handle or pointer or
whatever...).
at the moment i'm working with a Hashtable Int32 to Object, but i want to
optimize this. I know it is possible to work with pointers in unsafe code
blocks, but as far as i know these pointer remain valid only within these
blocks, or when they are pinned. and i don't want to disturb the garbage
collector.
i mean, somehow a reference must be a number anyway, perhaps an index within
some garbage collector table, or am i wrong?
 
As far as I know, there's no conversion between an object reference and an Int32, unless you pin objects using GCHandles and get their associated IntPtrs.

Also, if you're storing references in a Hashtable, then you are affecting the garbage collector by holding references to the objects, so they'll never get collected.

Even if references are stored internally as numbers in a GC table, there's no way for managed code to access it.

Hope that helps
-Chris

--------------------
i'm looking for a possibility in C# to get a unique Int32 handle for an
object reference. This handle must later (within the same process of course)
be convertible back to an object reference (call it handle or pointer or
whatever...).
at the moment i'm working with a Hashtable Int32 to Object, but i want to
optimize this. I know it is possible to work with pointers in unsafe code
blocks, but as far as i know these pointer remain valid only within these
blocks, or when they are pinned. and i don't want to disturb the garbage
collector.
i mean, somehow a reference must be a number anyway, perhaps an index within
some garbage collector table, or am i wrong?


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
unless you pin objects using GCHandles and get their associated IntPtrs.

You can cast a GCHandle to an IntPtr or void* (and back) even if it's
non-pinning.



Mattias
 
True, but then you're getting an IntPtr representation of the GCHandle, not the underlying object. Using GCHandle.AddrOfPinnedObject will give an IntPtr of the actual object.


--------------------
You can cast a GCHandle to an IntPtr or void* (and back) even if it's
non-pinning.



Mattias


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Back
Top