cppzmq icon indicating copy to clipboard operation
cppzmq copied to clipboard

using message_t

Open bertbenetton opened this issue 8 years ago • 4 comments

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..`` } `` } }

bertbenetton avatar Jun 30 '16 16:06 bertbenetton

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 avatar Aug 10 '16 08:08 K0n63n

@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 avatar Apr 04 '18 20:04 sigiesec

@sigiesec Alright, thanks for the clarification

K0n63n avatar Apr 05 '18 08:04 K0n63n

@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?

madjxatw avatar Aug 02 '23 11:08 madjxatw