cppzmq
cppzmq copied to clipboard
using message_t
Hello, it is possible to "reuse" message_t into a recv loop? For instance, is the followed code correct?..or message_t varible has to create and destroy on each loop iteration? so I have to declare message_t msg inside while loop.
I'm wondering if reusing messate_t could be any internal memory leak?
...
void ReadThread(..)
{
message_t msg; //reused varible `` //... some code
while(true) `` {
``
poll(&items[0], items.size(), 1000);``
if (items [0].revents & ZMQ_POLLIN)``
{ `` ``
service->recv(&msg);`` ``
// do something with msg..``
} `` }
}
Assuming that your "service" is a socket_t it will call zmq_msg_recv under the hood. Documentation for that function (http://api.zeromq.org/4-1:zmq-msg-recv) says: Any content previously stored in msg shall be properly deallocated.
So I think is best to declare the msg right before your
service->recv(&msg);
@K0n63n You misinterpret the docs of zmq_msg_recv. The "shall be properly deallocated" part describes what the implementation of zmq_msg_recv shall do, not what the caller shall do.
@sigiesec Alright, thanks for the clarification
@K0n63n You misinterpret the docs of zmq_msg_recv. The "shall be properly deallocated" part describes what the implementation of zmq_msg_recv shall do, not what the caller shall do.
so is the code in question workable?