Hope this time, my message is sent!!!

After MUCH digging, I have come into something that I would like you to
analyse..****** **

First, the scenario... As I told before, I replaced all my handles by a
derived class that I made called TSharedHandle, that is derived from

So, whenever I want to use messaging I just create my TSharedHandle
(Replacing HWND and AllocateHWnd) and use its messaging abilities derived
from your library

My problem is that sometimes I received a "Non Sent" message inside my
class. Obviously, the Windows handle acquired by my class is shared with
other instances of my class, and shared with your sockets.

After digging, I found that after a socket is destroyed (and closed),
windows might still sent messages to the registered handle, that you
previously registered with WSocket_Synchronized_WSAASyncSelect call.

I have found a way to solve the problem, and that is why I need your input.

Inside routine procedure TCustomWSocket.InternalClose(bShut : Boolean; Error:
Word) I have added the following line:

            WSocket_Synchronized_WSAASyncSelect(FHSocket,  Handle,  0,  0);*

** **

Just after****

    if FState = wsClosed then

** **

** **

I realized that if I unselect the messages using WSAASyncSelect, and
passing 0, 0, the problem will never happen.****

** **

Also, another thing, if I just call****

WSocket_Synchronized_WSAASyncSelect(FHSocket,  Handle,  0,  0) AFTER
iStatus := WSocket_Synchronized_closesocket(FHSocket), it will NOT work,
and the problem will persist****

Apparently, closing the socket using iStatus :=
WSocket_Synchronized_closesocket(FHSocket) does not guarantee that windows
will not send any more messages to the registered handle, and we have to
explicitly tell windows to not send any more socket messages using
WSocket_Synchronized_WSAASyncSelect(FHSocket,  Handle,  0,  0), but this
must be called before closing the socket

Instead of changing the library I just overwritten the InternalClose
routine in my derived socket class to make like this:****

**   // Inform socket to not send any more messages to the message handle
  if (FState <> wsClosed) and (FHSocket <> INVALID_SOCKET) then
    Ics_WSAAsyncSelect(FHSocket, Handle, 0, 0);

  inherited InternalClose(bShut, Error);

** **

And it worked..****

So, here is a little and simplistic flow of that I think it is happening

- Create a socket****

- Handles will be allocated to the socket, imagine that message 1028 is for
FMsg_WM_ASYNCSELECT and this message 1028 is now registered on winsock, so
winsock will send messages to ICS Socket handle****

- Perform some socket operation (Like connecting...)****

- Destroy socket class (It will actually release message 1028 and make it
available for next class)****

- Create a new class that uses shared handle****

- New class will receive ID 1028 for its internal messaging****

- Winsock sends a message to still existing handle (its shared right..)
with message ID 1028 (Don´t know which message could be sent after socket is


- This message will be received by the new class that is now assigned
message 1028 and will create the most unexpected behaviours!****

** **

** **

I´m pretty confident that this is what is happening, but it is really
difficult to simulate, I have to perform many operations simultaneously to
have this problem occurring...****

** **

So, if I tell winsock to not send any more socket messages just before
closing it, I won´t have any more issues...****

** **

What are your considerations to this?****

** **

Does that makes sense at all???****

** **


** **

Eric Fleming Bonilha****

To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to