Code Connected Volume 1 – page 47 have an example about how to receive multipart message:
while (1) {
zmq_msg_t message;
zmq_msg_init(&message);
zmq_msg_recv(&message, socket, 0);
// Process the message frame
...
zmq_msg_close(&message);
if (!zmq_msg_more(&message))
break;
}
Is this correct? Shouldn’t we use zmq_msg_close() after zmq_msg_more()?
The API reference for zmq_msg_more() and zmq_msg_recv() (ZeroMQ 3.2.2 Stable) both contain examples showing that zmq_msg_close() is called after zmq_msg_more. As far as I know the API docs do not specifically state anything to contradict this, thus the example from Code Connected seems wrong. The documentation for
zmq_msg_close()states that the actual memory release may be postponed by underlaying layers, implicating that thezmq_msg_more()operation may succeed but it still looks wrong to call it after closing the message.Example from zmq_msg_more() API documentation (3.2.2) (edited slightly for readability):
However, looking in the ZeroMq Guide regarding Multi-Part Messages, that example actually checks for more messages after closing the message, but that is achieved by checking the socket using zmq_getsockopt(), without using any references to the message. I suspect the Code Connected examples simply used that example and changed from
zmq_getsockopt()tozmq_msg_more()(probably incorrecly so).Example from ZeroMq Guide (multi-part messages):