Skip to content

Commit 8d36a7c

Browse files
committed
Problem: zmq_msg_t not closed on exception
Solution: Use zmq::message_t
1 parent f428fee commit 8d36a7c

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

zmq.hpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,8 +2309,7 @@ class monitor_t
23092309
{
23102310
assert(_monitor_socket);
23112311

2312-
zmq_msg_t eventMsg;
2313-
zmq_msg_init(&eventMsg);
2312+
zmq::message_t eventMsg;
23142313

23152314
zmq::pollitem_t items[] = {
23162315
{_monitor_socket.handle(), 0, ZMQ_POLLIN, 0},
@@ -2319,48 +2318,42 @@ class monitor_t
23192318
zmq::poll(&items[0], 1, timeout);
23202319

23212320
if (items[0].revents & ZMQ_POLLIN) {
2322-
int rc = zmq_msg_recv(&eventMsg, _monitor_socket.handle(), 0);
2321+
int rc = zmq_msg_recv(eventMsg.handle(), _monitor_socket.handle(), 0);
23232322
if (rc == -1 && zmq_errno() == ETERM)
23242323
return false;
23252324
assert(rc != -1);
23262325

23272326
} else {
2328-
zmq_msg_close(&eventMsg);
23292327
return false;
23302328
}
23312329

23322330
#if ZMQ_VERSION_MAJOR >= 4
2333-
const char *data = static_cast<const char *>(zmq_msg_data(&eventMsg));
2331+
const char *data = static_cast<const char *>(eventMsg.data());
23342332
zmq_event_t msgEvent;
23352333
memcpy(&msgEvent.event, data, sizeof(uint16_t));
23362334
data += sizeof(uint16_t);
23372335
memcpy(&msgEvent.value, data, sizeof(int32_t));
23382336
zmq_event_t *event = &msgEvent;
23392337
#else
2340-
zmq_event_t *event = static_cast<zmq_event_t *>(zmq_msg_data(&eventMsg));
2338+
zmq_event_t *event = static_cast<zmq_event_t *>(eventMsg.data());
23412339
#endif
23422340

23432341
#ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2344-
zmq_msg_t addrMsg;
2345-
zmq_msg_init(&addrMsg);
2346-
int rc = zmq_msg_recv(&addrMsg, _monitor_socket.handle(), 0);
2342+
zmq::message_t addrMsg;
2343+
int rc = zmq_msg_recv(addrMsg.handle(), _monitor_socket.handle(), 0);
23472344
if (rc == -1 && zmq_errno() == ETERM) {
2348-
zmq_msg_close(&eventMsg);
23492345
return false;
23502346
}
23512347

23522348
assert(rc != -1);
2353-
const char *str = static_cast<const char *>(zmq_msg_data(&addrMsg));
2354-
std::string address(str, str + zmq_msg_size(&addrMsg));
2355-
zmq_msg_close(&addrMsg);
2349+
std::string address = addrMsg.to_string();
23562350
#else
23572351
// 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.
23582352
std::string address = event->data.connected.addr;
23592353
#endif
23602354

23612355
#ifdef ZMQ_EVENT_MONITOR_STOPPED
23622356
if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2363-
zmq_msg_close(&eventMsg);
23642357
return false;
23652358
}
23662359

@@ -2424,7 +2417,6 @@ class monitor_t
24242417
on_event_unknown(*event, address.c_str());
24252418
break;
24262419
}
2427-
zmq_msg_close(&eventMsg);
24282420

24292421
return true;
24302422
}

0 commit comments

Comments
 (0)