Benny
Benny

Reputation: 8815

passing unmanaged pointer field contained in managed class to unmanaged code

I have a managed class which contains unmanaged class pointer:

class Managed { public IntPtr ptr; };

c++ function which takes a pointer as parameter:

void foo(void *ptr);

should i pin this Managed object before calling the unmanaged function?

calling code:

Managed obj = new Managed;
foo(obj.ptr);

Upvotes: 0

Views: 473

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500645

I don't see why you'd need to pin it - even if the GC moves obj itself, the value of obj.ptr shouldn't be affected - and obj.ptr is passed to your C++ code by value, so it's not like the C++ code can try to change the contents of obj.

Upvotes: 2

Related Questions