mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-12 18:40:28 +01:00
Merge pull request #482 from kevle/monitor-check_event-raii
Fix for potential memory leak in monitor_t::check_event
This commit is contained in:
commit
dc4c0656a6
22
zmq.hpp
22
zmq.hpp
@ -2309,8 +2309,7 @@ class monitor_t
|
|||||||
{
|
{
|
||||||
assert(_monitor_socket);
|
assert(_monitor_socket);
|
||||||
|
|
||||||
zmq_msg_t eventMsg;
|
zmq::message_t eventMsg;
|
||||||
zmq_msg_init(&eventMsg);
|
|
||||||
|
|
||||||
zmq::pollitem_t items[] = {
|
zmq::pollitem_t items[] = {
|
||||||
{_monitor_socket.handle(), 0, ZMQ_POLLIN, 0},
|
{_monitor_socket.handle(), 0, ZMQ_POLLIN, 0},
|
||||||
@ -2319,40 +2318,35 @@ class monitor_t
|
|||||||
zmq::poll(&items[0], 1, timeout);
|
zmq::poll(&items[0], 1, timeout);
|
||||||
|
|
||||||
if (items[0].revents & ZMQ_POLLIN) {
|
if (items[0].revents & ZMQ_POLLIN) {
|
||||||
int rc = zmq_msg_recv(&eventMsg, _monitor_socket.handle(), 0);
|
int rc = zmq_msg_recv(eventMsg.handle(), _monitor_socket.handle(), 0);
|
||||||
if (rc == -1 && zmq_errno() == ETERM)
|
if (rc == -1 && zmq_errno() == ETERM)
|
||||||
return false;
|
return false;
|
||||||
assert(rc != -1);
|
assert(rc != -1);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
zmq_msg_close(&eventMsg);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ZMQ_VERSION_MAJOR >= 4
|
#if ZMQ_VERSION_MAJOR >= 4
|
||||||
const char *data = static_cast<const char *>(zmq_msg_data(&eventMsg));
|
const char *data = static_cast<const char *>(eventMsg.data());
|
||||||
zmq_event_t msgEvent;
|
zmq_event_t msgEvent;
|
||||||
memcpy(&msgEvent.event, data, sizeof(uint16_t));
|
memcpy(&msgEvent.event, data, sizeof(uint16_t));
|
||||||
data += sizeof(uint16_t);
|
data += sizeof(uint16_t);
|
||||||
memcpy(&msgEvent.value, data, sizeof(int32_t));
|
memcpy(&msgEvent.value, data, sizeof(int32_t));
|
||||||
zmq_event_t *event = &msgEvent;
|
zmq_event_t *event = &msgEvent;
|
||||||
#else
|
#else
|
||||||
zmq_event_t *event = static_cast<zmq_event_t *>(zmq_msg_data(&eventMsg));
|
zmq_event_t *event = static_cast<zmq_event_t *>(eventMsg.data());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
|
#ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
|
||||||
zmq_msg_t addrMsg;
|
zmq::message_t addrMsg;
|
||||||
zmq_msg_init(&addrMsg);
|
int rc = zmq_msg_recv(addrMsg.handle(), _monitor_socket.handle(), 0);
|
||||||
int rc = zmq_msg_recv(&addrMsg, _monitor_socket.handle(), 0);
|
|
||||||
if (rc == -1 && zmq_errno() == ETERM) {
|
if (rc == -1 && zmq_errno() == ETERM) {
|
||||||
zmq_msg_close(&eventMsg);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(rc != -1);
|
assert(rc != -1);
|
||||||
const char *str = static_cast<const char *>(zmq_msg_data(&addrMsg));
|
std::string address = addrMsg.to_string();
|
||||||
std::string address(str, str + zmq_msg_size(&addrMsg));
|
|
||||||
zmq_msg_close(&addrMsg);
|
|
||||||
#else
|
#else
|
||||||
// Bit of a hack, but all events in the zmq_event_t union have the same layout so this will work for all event types.
|
// Bit of a hack, but all events in the zmq_event_t union have the same layout so this will work for all event types.
|
||||||
std::string address = event->data.connected.addr;
|
std::string address = event->data.connected.addr;
|
||||||
@ -2360,7 +2354,6 @@ class monitor_t
|
|||||||
|
|
||||||
#ifdef ZMQ_EVENT_MONITOR_STOPPED
|
#ifdef ZMQ_EVENT_MONITOR_STOPPED
|
||||||
if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
|
if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
|
||||||
zmq_msg_close(&eventMsg);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2424,7 +2417,6 @@ class monitor_t
|
|||||||
on_event_unknown(*event, address.c_str());
|
on_event_unknown(*event, address.c_str());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
zmq_msg_close(&eventMsg);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user