RE: When do I need to pin?
- From: jialge@xxxxxxxxxxxxxxxxxxxx ("Jialiang Ge [MSFT]")
- Date: Fri, 03 Oct 2008 04:51:07 GMT
Hello Nick
The part where you say 'CLR may do
it for us' is a bit disturbing. Will it pin for us or not?
It's a very sharp and acute observation. Thank you for reminding me. I
should have explained it in the first reply.
I said 'may' because it depends on whether it is a synchronous or a
asynchronous I/O operation:
Synchronous and Asynchronous I/O
http://msdn.microsoft.com/en-us/library/aa365683(VS.85).aspx
When FILE_FLAG_OVERLAPPED is not specified in the dwFlagsAndAttributes
parameter of the CreateFile call, it's a synchronous IO. CLR will help us
to pin the buffer.
But when we have to do asynchronous IO operations (FILE_FLAG_OVERLAPPED is
specified in dwFlagsAndAttributes), we "MAY" need to manually pin the
buffer because you pass the address to the buffer down and the API returns
before it has completed using the buffer. Then, when the I/O completes, you
unpin the buffer manually. I say "MAY" because CLR provides basic interop
with the native async IO including automatic pinning the user buffers. The
related CLR class is called Overlapped. For the said interop to work you
have to invoke Overlapped.Pack (or UnsafePack) method. The
Overlapped.Pack(IoCompletionCallback iocb, Object data) overload expects
one to pass "data" as either byte[] or object[] type. If it's byte[] then
the Pack() method will pin that byte array. Otherwise it will pin all
members of the object[] array. The data gets un-pinned when IO completes:
<quote>
Overlapped.Pack Method (IOCompletionCallback)
http://msdn.microsoft.com/en-us/library/1d949965.aspx
The caller is responsible for pinning the buffer. If the application domain
is unloaded, however, the handle to the pinned buffer is destroyed and the
buffer is released, leaving the I/O operation to write to the freed
address. For this reason, it is better to use the
Pack(IOCompletionCallback, Object) method overload, in which the runtime
pins the buffer.
Overlapped.Pack Method (IOCompletionCallback, Object)
http://msdn.microsoft.com/en-us/library/a059s071.aspx
The runtime pins the buffer or buffers specified in userData for the
duration of the I/O operation. If the application domain is unloaded, the
runtime keeps the memory pinned until the I/O operation completes.
</quote>
If you use the built-in BeginXxx/EndXxx methods to do async I/O, then these
methods do the pinning/unpinning for you automatically.
For more readings:
Asynchronous Device Operations
http://msdn.microsoft.com/en-us/magazine/cc163415.aspx
Pop Quiz Pins: Answers by Greg Young [MVP]
http://codebetter.com/blogs/gregyoung/archive/2007/08/16/pop-quiz-pins-answe
rs.aspx
Another situation in which manual pinning an object is required is that we
may want to continue pinning the object after the native function returns:
(quoted from http://msdn.microsoft.com/en-us/magazine/cc163910.aspx)
When the runtime marshaler sees that your code is passing to native code a
reference to a managed reference object, it automatically pins the object.
When the native function returns, the marshaled object is automatically
unpinned. Automatic pinning is very convenient, but it raises another
question. What happens if the native function caches the pointer for use
later on? When the function returns, won't the collector be free to move
the object? The answer is yes, and the solution for your code in such a
situation is to manually pin the object using the
System.Runtime.InteropServices.GCHandle type.
I hope that the above info is easy to understand. If you find anything
unclear, let me know and I will give more explanation or examples.
Regards,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
.
- Follow-Ups:
- RE: When do I need to pin?
- From: nickdu
- RE: When do I need to pin?
- References:
- When do I need to pin?
- From: nickdu
- RE: When do I need to pin?
- From: "Jialiang Ge [MSFT]"
- RE: When do I need to pin?
- From: nickdu
- When do I need to pin?
- Prev by Date: RE: When do I need to pin?
- Next by Date: how to create a Net assembly as replacement for existing COM component
- Previous by thread: RE: When do I need to pin?
- Next by thread: RE: When do I need to pin?
- Index(es):
Relevant Pages
|
Loading