Hi,
I have got my hands on a managed object pointer (no its not a GCHandle) and want to convert it back to its true object. Before you ask:
- Yes this is unsafe
- GC might have moved the pointer to the managed object in the meantime rendering my pointer useless
I have ensured that at this very moment I have a valid pointer so how can I make an object out of it?
My current approach doesnot play well with the GC
IntPtr pManagedObject = xxx
object []arr = new object[1];
IntPtr pArr = Marshal.UnsafeAddrOfPinnedArrayElement(arr,0);
Marshal.WriteIntPtr(pArr,pManagedObject);
The problem is that WriteIntPtr does write to a managed object which is not pinned. So it can happen that I write to an arbitrary memory location which could belong to a totally different object when a GC happens betwee UnsafeAddrOf.. and the WriteIntPtr call. Since GCHandle does not allow pinning of a managed object I am running out of options. All other attempts to Marshal it e.g. as a structure are prevented by the error message that the structure contains non blitable types. Are there any options which do play well with the GC?
Yours,
Alois Kraus