Commit 5fead473 authored by Joe Eli McIlvain's avatar Joe Eli McIlvain

Merge pull request #1482 from brian-peloton/master

Fix memory leak when creating a socket fails.
parents c05a475b 773a06fc
...@@ -133,12 +133,16 @@ zmq::signaler_t::signaler_t () ...@@ -133,12 +133,16 @@ zmq::signaler_t::signaler_t ()
#endif #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 () zmq::signaler_t::~signaler_t ()
{ {
#if defined ZMQ_HAVE_EVENTFD #if defined ZMQ_HAVE_EVENTFD
if (r == retired_fd) return;
int rc = close_wait_ms (r); int rc = close_wait_ms (r);
errno_assert (rc == 0); errno_assert (rc == 0);
#elif defined ZMQ_HAVE_WINDOWS #elif defined ZMQ_HAVE_WINDOWS
if (w != retired_fd) {
const struct linger so_linger = { 1, 0 }; const struct linger so_linger = { 1, 0 };
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER, int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
(const char *) &so_linger, sizeof so_linger); (const char *) &so_linger, sizeof so_linger);
...@@ -147,14 +151,19 @@ zmq::signaler_t::~signaler_t () ...@@ -147,14 +151,19 @@ zmq::signaler_t::~signaler_t ()
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
rc = closesocket (w); rc = closesocket (w);
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
if (r == retired_fd) return;
rc = closesocket (r); rc = closesocket (r);
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
} }
#else #else
if (w != retired_fd) {
int rc = close_wait_ms (w); int rc = close_wait_ms (w);
errno_assert (rc == 0); errno_assert (rc == 0);
}
if (r != retired_fd) {
rc = close_wait_ms (r); rc = close_wait_ms (r);
errno_assert (rc == 0); errno_assert (rc == 0);
}
#endif #endif
} }
......
...@@ -154,8 +154,11 @@ zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_, ...@@ -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); 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 NULL;
}
return s; return s;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment