[zeromq-dev] Use of the Zero-Copy Message Constructor?

Bob Beaty drbobbeaty at gmail.com
Mon Feb 14 15:04:02 CET 2011

  I've been experimenting with the "zero copy" zmq::message_t constructor in C++ and I'm a little surprised that it's not properly respecting the 'size' parameter of the constructor.
  My original code, that works just fine, is:

    try {
      zmq::message_t  msg(aPayload.size());
      memcpy(msg.data(), aPayload.data(), aPayload.size());
    } catch (std::exception & e) {

where the 'aPayload' was a std::string with the contents of the message. Pretty simple.
  When I changed the code to:

    try {
      zmq::message_t  msg((void *)aPayload.data(),
                          aPayload.size(), recycle);
    } catch (std::exception & e) {

where 'aPayload' is still the std::string, and the function 'recycle' is just a static method on my class:

    static void recycle( void *data, void *hint );

and it all compiles and runs. So far so good.
  The receiver has some problems, however. I haven't changed the code there when I made this change, so it's fixed. But now when I receive the messages, I'm seeing that they are off by a byte or two - more or less, seemingly randomly. I include the expected message size in the message body as a check, which is how I know how big it's *supposed* to be.
  So the question is: Am I doing this right?
  If so, then what can I do to get the zero-copy working?
  For now, I've reverted to the first version and all is working just fine.

        Bob (drbobbeaty at gmail.com)
    The Man from S.P.U.D.
    We will write no code before it's designed.

More information about the zeromq-dev mailing list