Merge pull request #1482 from brian-peloton/master

Fix memory leak when creating a socket fails.
This commit is contained in:
Joe Eli McIlvain 2015-07-22 11:39:39 -07:00
commit 5fead473a4
2 changed files with 28 additions and 16 deletions

View File

@ -133,28 +133,37 @@ zmq::signaler_t::signaler_t ()
#endif
}
// This might get run after some part of construction failed, leaving one or
// both of r and w retired_fd.
zmq::signaler_t::~signaler_t ()
{
#if defined ZMQ_HAVE_EVENTFD
if (r == retired_fd) return;
int rc = close_wait_ms (r);
errno_assert (rc == 0);
#elif defined ZMQ_HAVE_WINDOWS
const struct linger so_linger = { 1, 0 };
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
(const char *) &so_linger, sizeof so_linger);
// Only check shutdown if WSASTARTUP was previously done
if (rc == 0 || WSAGetLastError () != WSANOTINITIALISED) {
wsa_assert (rc != SOCKET_ERROR);
rc = closesocket (w);
wsa_assert (rc != SOCKET_ERROR);
rc = closesocket (r);
wsa_assert (rc != SOCKET_ERROR);
}
if (w != retired_fd) {
const struct linger so_linger = { 1, 0 };
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
(const char *) &so_linger, sizeof so_linger);
// Only check shutdown if WSASTARTUP was previously done
if (rc == 0 || WSAGetLastError () != WSANOTINITIALISED) {
wsa_assert (rc != SOCKET_ERROR);
rc = closesocket (w);
wsa_assert (rc != SOCKET_ERROR);
if (r == retired_fd) return;
rc = closesocket (r);
wsa_assert (rc != SOCKET_ERROR);
}
#else
int rc = close_wait_ms (w);
errno_assert (rc == 0);
rc = close_wait_ms (r);
errno_assert (rc == 0);
if (w != retired_fd) {
int rc = close_wait_ms (w);
errno_assert (rc == 0);
}
if (r != retired_fd) {
rc = close_wait_ms (r);
errno_assert (rc == 0);
}
#endif
}

View File

@ -154,8 +154,11 @@ zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_,
mailbox_t *mailbox = dynamic_cast<mailbox_t*> (s->mailbox);
if (mailbox != NULL && mailbox->get_fd () == retired_fd)
if (mailbox != NULL && mailbox->get_fd () == retired_fd) {
s->destroyed = true;
delete s;
return NULL;
}
return s;
}