I think that zmq::message_t m((void *) std::move(msg.second).data()...
is probably undefined behaviour, but is certainly the cause of your problem. In this instance, std::move
isn't doing what I suspect you think it does.
The call to std::move
is effectively creating an anonymous temporary of a string, moving the contents of msg.second
into it, then passing a pointer to that temporary data into the message_t
constructor. The 0MQ code assumes that pointer is valid, but the temporary object is destroyed after the constructor of message_t
completes - i.e. before you call send_frame
.
Zero-copy is a complicated matter in 0mq (see the 0MQ Guide) for more details, but you have to ensure that the data that hasn't been copied is valid until 0MQ tells you explicitly that it's finished with it.
Using C++ strings in this situation is hard, and requires a lot of thought. Your question about how to "avoid that the string is deallocated..." goes right to the heart of the issue. The only answer to that is "with great care".
In short, are you sure you need zero-copy at all?