[zeromq-dev] question about message buffer
Mohit Jaggi
mohitjaggi at gmail.com
Mon Mar 4 19:59:29 CET 2013
This is not fully full but more detailed...'buf' is on stack (so destructor
will be called) and 'm' is on heap(and I havn't called delete...should I?).
I want to understand how zmq treats them.
context_t ctx(1);
socket_t *publisher;
void init() {
publisher = new socket....
publisher.bind(...)
}
void publish(myObject) {
string buf;
myObject.SerializeToString(&buf);
message_t m = new message_t(buf.size());
memcpy((*void**) m.data(), buf.c_str(), buf.size());
string topic(string("X") + string("Y") + string("Z"));
publisher->send(topic.c_str(), topic.size(), ZMQ_SNDMORE); //topic
publisher->send(m); //data
}
main() {
init();
...
publish(obj);
}
On Mon, Mar 4, 2013 at 12:51 AM, Pieter Hintjens <ph at imatix.com> wrote:
> Hi Mohit,
>
> Can you provide a full example?
>
> -Pieter
>
>
> On Mon, Mar 4, 2013 at 8:34 AM, Mohit Jaggi <mohitjaggi at gmail.com> wrote:
> > Hi,
> > I am a little confused about how messages and buffers are handled in
> zmq. In
> > the following example, valgrind finds a message leak for 'm'.
> > 1) Should I call delete for 'm' after the send?
> > 2) zmq will keep the buffer(data) but free memory for 'm' if I do this?
> > 3) What about 'topic'? It is on stack and data is small, so I guess it
> uses
> > the vsm area to store the message?
> > 4) Will send for 'topic' make a copy of the data then?
> >
> > Mohit.
> >
> >
> > string buf;
> >
> > myObject.SerializeToString(&buf);
> >
> > message_t m = new message_t(buf.size());
> >
> > memcpy((void*) m.data(), buf.c_str(), buf.size());
> >
> > string topic(string("X") + string("Y") + string("Z"));
> >
> > publisher->send(topic.c_str(), topic.size(), ZMQ_SNDMORE); //topic
> >
> > publisher->send(m); //data
> >
> >
> > _______________________________________________
> > zeromq-dev mailing list
> > zeromq-dev at lists.zeromq.org
> > http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> >
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130304/814f4443/attachment.htm>
More information about the zeromq-dev
mailing list