[zeromq-dev] zmq_msg_init_data with zero deallocation function
Dirkjan Ochtman
dirkjan at ochtman.nl
Wed May 11 16:18:10 CEST 2011
On Wed, May 11, 2011 at 15:38, Ilja Golshtein <ilejncs at narod.ru> wrote:
> According to zmq_msg_init_data manual
> ==
> int zmq_msg_init_data (zmq_msg_t *msg, void *data, size_t size, zmq_free_fn *ffn, void *hint);
> ==
> ffn is optional ("If provided, the deallocation function ffn shall be called once the data buffer is no longer required by 0MQ").
>
> How does the thing work if ffn is not provided?
If ffn is not provided, the object passed in will never be freed.
> Is the code
> ==
> zmq_msg_t msg;
> zmq_msg_init_data (&msg, (void *)"something", 9, NULL, NULL);
> ==
> valid?
I think in this case "something" is allocated on the stack. If the
message stays in the queue for a little bit (which might happen at any
time), the stack might've gone away and 0MQ is trying to read memory
that might have been overwritten. In almost all of the cases, you
should just use zmq_msg_init_size() and use memcpy() to copy your data
(e.g. "something") into zmq_msg_data(&msg). The guide explains this
quite well.
Cheers,
Dirkjan
More information about the zeromq-dev
mailing list