IO Completion Ports and OVERLAPPED management

c++, iocp, winapi

Solution

The `OVERLAPPED` structure must exist from when a successful I/O operation (or manual PostQueuedCompletionStatus()) executes until the `OVERLAPPED` emerges from a call to GetQueuedCompletionStatus().

You are responsible for the lifetime of the structure.

You'll see from the MSDN docs that `GetQueuedCompletionStatus()` actually takes "a pointer to a variable that receives the address of the `OVERLAPPED` structure that was specified when the completed I/O operation was started.". What you actually get out of that call is a pointer to the original `OVERLAPPED` that you passed when you made the `PostQueuedCompletionStatus()` call (or initiated an overlapped I/O operation).

This is all actually very useful as the "normal" way to use the `OVERLAPPED` structure is to place it inside a larger structure which holds all of the 'per operation' information that you might need - so it's the ideal way to navigate directly from the limited information that you're given when you call `GetQueuedCompletionStatus()` to, for example, the data buffer that you used in your overlapped read call...

I find the best way to deal with `OVERLAPPED` structures is to a) embed them in the buffer you're using for read/write b) reference count them and c) return them to a pool for reuse when the ref count drops to 0.

I have some source code that you could download (here) which may make this a little easier to understand (it's a full IOCP server example so it's a little complex, but it works and shows how these things can be used).

Problem

How win32 manages instances of OVERLAPPED struct in context of two functions: ``` GetQueuedCompletionStatus PostQueuedCompletionStatus ``` When I call GetQueuedCompletionStatus does win32 free instance of OVERLAPPED struct or I must do it by my self? When I send data with PostQueuedCompletionStatus does win32 copy it to internal structs? When I must free memory of sent data? Where I could find some picture with scheme of processing of OVERLAPPED data between GetQueuedCompletionStatus, PostQueuedCompletionStatus and IOCP queue?

Original source