Re: IOCP touching my OVERLAPPED after my Write has completed



Hi mate

Anton is telling you that the memory must not be freed (or accessed) between
the call to WSASend etc. and the return of GQCS.


The only thing I am saying is that you should not invalidate pointers
that have been passed to your code, unless you know for sure what the
caller does with them after your function returns - by doing such
things you may introduce really insidious bugs that may stay undetected
for quite a while. On this particular occasion the OP was just lucky -
his program just crashed and burned right on the spot.


Anton Bassov


m wrote:
No you have not misinterpreted the MSDN documentation. After the call to
GQCS returns the IO operation is complete and the memory associated can be
freed or reused.



Anton is telling you that the memory must not be freed (or accessed) between
the call to WSASend etc. and the return of GQCS.


"Alberto Demichelis" <AlbertoDemichelis@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:5444A636-B03E-4283-BD9C-7A7BEC8938BC@xxxxxxxxxxxxxxxx
First, thanks for all the time you are spending on my issue.

I must have misinterpreted this sentence from MSDN:
"The lpOverlapped parameter must be valid for the duration of the
overlapped
operation."
I supposed the completion of the request is the end of the overlapped
operation.
But if this is the expected behaviour then they should call it 'almost
completion routine'.

The case 2 you are proposing, is exactly what I'm doing and is the source
of
my 'bug'. The fact that I protect the memory is just to debug the system.
I cannot mark the memory as free and put it back in my pool when the IO
completes. If another worker fretches it back from the pool before winsock
is
done with it, bad thing could happen(guess).

To mee seems that I have to emply some form of refcounting to be safe.

ciao
Alberto

"anton bassov" wrote:


What are these samples?????

Try to look at the situation from the caller's (i.e.Winsock) point of
view. Once you have passed a perfectly valid pointer to some function,
you may want to check the pointee
after the function returns. Therefore, it would not be too unreasonable
for you to expect
this pointer to remain valid upon the function's return, don't you
think?????? If you are still not convinced, I have to remind you that
in C language we pass arguments by value,
so that we expect them to be immutable. The only thing you can change
is pointee, but pointer in itself should stay the same. In other words,
what you are doing is very, very bad programming technique in itself,
and you should avoid doing things like that.

In your particular situation I see 2 possible solutions:

1. Don't use the completion routine at all - just pass unsignalled
event in OVERLAPPED structure, and make some other thread wait on it.
When IO operation gets completed, this event will get signalled, and,
at this point, you will be able to safely free memory


2. Don't free memory, but, instead, just mark it as available - you
will be able to reuse it upon the next send or receive operation. As a
result, you will avoid unnecessary calls to VirtualAlloc() and
VirtualFree(), and, hence, improve your performance (this is
particularly true if your program sends and receives data frequently)

Anton Bassov



Alberto Demichelis wrote:
yes I'm decomminting in the completion routine. I've seen that all
examples
do some form of release in there.

Alberto

"anton bassov" wrote:

Hi Alberto

When the request completes I uncommit the page

Are you doing it in IO completion routine?

Anton Bassov


Alberto Demichelis wrote:
Hi, I've recently posted about a crash I was experiencing in my
server on
heavy load.
Recently I've rewritten part of my IOCP routines and added some
extra
consistency checks. While doing this I've
realized that the my OVERLAPPED struct gets read by winsock after
the IO
request has completed. That in my current implementation means that
is
reading freed memory.

This is my scenario. I have a IO request pool implemented using
VirtualAlloc.
I reserve few megs of virtual memory, every time I allocate a
request I
commit a page and put all my IO structs in it.
When the request completes I uncommit the page.

The result is an access violation in WSAWriteTo() [Access violation
reading
location 0x1185d004]. This happens when the IO request completes
before
WSAWriteTo() returns.
Probably the same happens in my ReadFrom but apparently it never
gets
triggered beacuse I deallocate my read requests in my main app
thread after
processing.

Now I'm a bit lost, all the IOCP samples I found on the net use the
same
pattern I use. When can I safely
release my OVERLAPPED and related buffers? I thought was 'on
completion' by
apparently I'm wrong.

thanks for your time
Alberto





.



Relevant Pages

  • Re: IOCP touching my OVERLAPPED after my Write has completed
    ... GQCS returns the IO operation is complete and the memory associated can ... I supposed the completion of the request is the end of the overlapped ... Once you have passed a perfectly valid pointer to some function, ... Anton Bassov ...
    (microsoft.public.win32.programmer.kernel)
  • Re: IOCP touching my OVERLAPPED after my Write has completed
    ... I supposed the completion of the request is the end of the overlapped ... I cannot mark the memory as free and put it back in my pool when the IO ... Once you have passed a perfectly valid pointer to some function, ... I have a IO request pool implemented using VirtualAlloc. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: IOCP touching my OVERLAPPED after my Write has completed
    ... Your code will leak memory in this case. ... The short answer is that when the call to GQCS returns the buffers ... I supposed the completion of the request is the end of the overlapped ... Once you have passed a perfectly valid pointer to some function, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: IOCP touching my OVERLAPPED after my Write has completed
    ... GQCS returns the IO operation is complete and the memory associated can be ... I supposed the completion of the request is the end of the overlapped ... Once you have passed a perfectly valid pointer to some function, ... Anton Bassov ...
    (microsoft.public.win32.programmer.kernel)
  • Re: IOCP touching my OVERLAPPED after my Write has completed
    ... Your code will leak memory in this case. ... I supposed the completion of the request is the end of the overlapped ... Once you have passed a perfectly valid pointer to some function, ... Anton Bassov ...
    (microsoft.public.win32.programmer.kernel)

Loading