david-maw / StreamSSL

The StreamSSL sample described in CodeProject
Other
47 stars 24 forks source link

CActiveSock should close the socket in the dtor and in Close ... #43

Closed 0ric1 closed 5 years ago

0ric1 commented 5 years ago

The dtor of CActiveSock should close the socket in any case and not only in the case if shutdown failed, because any resources attached to the socket will not be freed until closesocket is invoked. This caused problems on my application when logging in / out on different servers, because some resources were not freed, see https://docs.microsoft.com/de-de/windows/win32/api/winsock/nf-winsock-closesocket : An application should always have a matching call to closesocket for each successful call to socket to return any socket resources to the system. I changed the close to this and for me it works fine now: `bool CActiveSock::Close() { if (ActualSocket == INVALID_SOCKET) { LastError = ERROR_HANDLES_CLOSED; return false; }

if (!WSACloseEvent(read_event)) { LastError = ::WSAGetLastError(); return false; }

if (!WSACloseEvent(write_event)) { LastError = ::WSAGetLastError(); return false; }

if (!CloseAndInvalidateSocket()) { LastError = ::WSAGetLastError(); return false; }

if (!WSACleanup()) { LastError = ::WSAGetLastError(); return false; }

return true; } `