【发布时间】:2019-10-29 20:20:53
【问题描述】:
我正在使用 ZeroMQ 来实现一个玩具通信协议;这是我第一次使用这个框架/库。
现在,在我的协议中,多条连续消息由某一方发送,所有消息都具有相同的大小。所以 - 我想,我会避免重新分配它们,而只是尝试用不同的内容重新填充消息数据缓冲区,例如:
zmq::message_t msg { fixed_common_size };
while (some_condition()) {
my_filling_routine(msg.data(), fixed_common_size);
the_socket.send(msg);
}
但是在这个循环的第二次迭代中,我得到了一个分段错误; msg.data() 不是 nullptr。我突然想到,ZeroMQ 可能会以某种方式蚕食内存,因此我需要编写如下内容:
zmq::message_t msg { fixed_common_size };
char buffer[fixed_common_size];
while (some_condition()) {
my_filling_routine(buffer, fixed_common_size);
msg.rebuild(buffer, fixed_common_size);
the_socket.send(msg);
}
但我确信这会导致取消分配和重新分配。
那么,rebuild() 真的是必要的吗,还是只是我的代码中的一些错误?
注意:我使用的是 Unix 套接字,以防答案取决于此。
【问题讨论】:
标签: segmentation-fault ipc zeromq