Boost json序列化和message_queue segfault
boost json serialization and message_queue segfault
我正在用boost进程间和ptree结构进行一些测试,当我试图读取发送的消息(或当我尝试在json中解析它时)时,我有一个段错误。
我在debian linux上使用boost1.49。
我在json中序列化它以供以后使用,因为我没有找到任何好的文档来直接序列化boost属性3。
这是我用来测试的代码(逗号表示segfault在哪里):
recv.cc
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <sstream>
struct test_data{
std::string action;
std::string name;
int faceID;
uint32_t Flags;
uint32_t freshness;
};
test_data recvData()
{
boost::interprocess::message_queue::remove("queue");
boost::property_tree::ptree pt;
test_data data;
std::istringstream buffer;
boost::interprocess::message_queue mq(boost::interprocess::open_or_create,"queue", 1, sizeof(buffer)
boost::interprocess::message_queue::size_type recvd_size;
unsigned int pri;
mq.receive(&buffer,sizeof(buffer),recvd_size,pri);
std::cout << buffer.str() << std::endl; //the segfault is there
boost::property_tree::read_json(buffer,pt);
data.action = pt.get<std::string>("action");
data.name = pt.get<std::string>("name");
data.faceID = pt.get<int>("face");
data.Flags = pt.get<uint32_t>("flags");
data.freshness = pt.get<uint32_t>("freshness");
boost::interprocess::message_queue::remove("queue");
return data;
}
int main()
{
test_data test;
test = recvData();
std::cout << test.action << test.name << test.faceID << test.Flags << test.freshness << std::endl;
}
sender.cc
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <sstream>
struct test_data{
std::string action;
std::string name;
int faceID;
uint32_t Flags;
uint32_t freshness;
};
int sendData(test_data data)
{
boost::property_tree::ptree pt;
pt.put("action",data.action);
pt.put("name",data.name);
pt.put("face",data.faceID);
pt.put("flags",data.Flags);
pt.put("freshness",data.freshness);
std::ostringstream buffer;
boost::property_tree::write_json(buffer,pt,false);
boost::interprocess::message_queue mq(boost::interprocess::open_only,"chiappen")
std::cout << sizeof(buffer) << std::endl;
mq.send(&buffer,sizeof(buffer),0);
return 0;
}
int main ()
{
test_data prova;
prova.action = "registration";
prova.name = "prefix";
prova.Flags = 0;
prova.freshness = 52;
sendData(prova);
}
我知道现在回答有点晚,但无论如何…你不能传递istringstream作为接收的缓冲区。Boost消息队列只处理原始字节,不像对象那样处理std。
要使其工作,必须使用char数组或先前使用malloc保留的任何缓冲区。
,
char buffer [1024];
mq.receive(buffer, sizeof(buffer), recvd_size, pri);
对于发送也是一样的,你只能发送原始字节,所以你不能使用ostringstream。
希望能有所帮助。
相关文章:
- C++中带有List类的迭代器Segfault
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- std::partition segfault issue
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- OpenSSL: EC_POINT_set_compressed_coordinates_GFp segfault
- C++ queue.front();为什么不从第一个元素开始呢?
- 检查nullptr是否100%保护内存布局不受segfault影响
- OpenCV Tracker 属性访问在 ARM 上因 SEGFAULT 而失败,但在 X86_64 中工作
- 我可以擦除 std::queue 中间的节点吗?
- 为什么优化大型 std::vector 数组会导致 SegFault?
- C++segfault,可重复的例子
- 带有zip_source_buffer的libzip会导致数据损坏和/或segfault
- 导致SegFault C++的析构函数
- 从引用的Vector获取SEGFAULT
- 尝试读取/写入Graphviz DAG值的工作证明会导致segfault
- Segfault如果更改派生类的指针值
- 获取大小时是否必须锁定 std::queue?
- SegFault deleting QTreeWidgetItem