Skip to content

How to detect if an object has been pinned #65927

Answered by stephentoub
msedi asked this question in Q&A
Discussion options

You must be logged in to vote

You don't. If you need it to be pinned, you need to pin it. The alternative is you document that it must be pinned and leave it up to the caller to ensure it, being very clear about how dangerous the API is, or force the user to pin if necessary by accepting a pointer instead of a managed reference. You could also accept a Memory<T>, which supports carrying a bit in it that let's the user say that it was created with a pinned object, and then when you call Memory.Pin, it won't create another GCHandle. Such instances can be created with MemoryMarshal.CreateFromPinnedArray.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@msedi
Comment options

@stephentoub
Comment options

Answer selected by msedi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants