diff --git a/src/common/socket.cpp b/src/common/socket.cpp index bcb331fcc3..b39c9c6752 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -38,6 +38,7 @@ #include "wx/apptrait.h" #include "wx/sckaddr.h" +#include "wx/scopeguard.h" #include "wx/stopwatch.h" #include "wx/thread.h" #include "wx/evtloop.h" @@ -527,6 +528,8 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) WX_SOCKLEN_T fromlen = sizeof(from); const wxSOCKET_T fd = accept(m_fd, &from.addr, &fromlen); + wxScopeGuard closeSocket = wxMakeGuard(close, fd); + // accepting is similar to reading in the sense that it resets "ready for // read" flag on the socket ReenableEvents(wxSOCKET_INPUT_FLAG); @@ -542,6 +545,8 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) if ( !sock ) return NULL; + // Ownership of the socket now passes to wxSocketImpl object. + closeSocket.Dismiss(); sock->m_fd = fd; sock->m_peer = wxSockAddressImpl(from.addr, fromlen);