c++asynchronouswinapioverlapped-ioio-completion-ports

Is it possible to recover an allocated OVERLAPPED structure used in a pending I/O operation?


Let's say you're doing an asynchronous operation (like ReadDirectoryChangesW) using I/O completion ports. And for each call to the function, you allocate an OVERLAPPED structure (perhaps with some additional data) for use within the I/O completion callback. And then within the callback, after the OVERLAPPED structure has been used, you free the memory using the pointer provided as an argument.

Now let's say it's time to shut everything down and cancel any pending asynchronous calls. Is there a function you can call to retrieve a pointer to the OVERLAPPED structure used in any currently pending I/O operation, so that you can free the memory?


Solution

  • Is there a function you can call to retrieve a pointer to the OVERLAPPED structure used in any currently pending I/O operation

    No, there is not. It is your responsibility to keep track of your allocated OVERLAPPEDs.

    However, when you cancel an asynchronous I/O operation, you will still receive a completion notification for it, indicating that the requested operation was aborted. So, if you are simply allocating your OVERLAPPEDs and passing them to the IOCP without keeping track of them, then you can continue freeing their memory in your notification handler, like you normally would. Simply don't fully shutdown until you have received a completion notification for every I/O operation that you have pending.