【问题标题】:boost json serialization and message_queue segfault提升 json 序列化和 message_queue 段错误
【发布时间】:2013-04-08 07:36:26
【问题描述】:

我正在使用 boost 进程间和 ptree 结构进行一些测试,当我尝试读取发送的消息时(或者当我尝试在 json 中解析它时)出现段错误。

我在 debian linux 上使用 boost1.49。

我在 json 中序列化它以供以后使用,因为我没有找到任何用于直接序列化 boost 属性 threes 的好文档。

这是我用来测试的代码(常见的说段错误在哪里):

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;
}   

发件人.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);
}

【问题讨论】:

  • 我相信这是我对 std:stringstream 对象的理解不正确

标签: c++ serialization message-queue boost-interprocess boost-propertytree


【解决方案1】:

我知道现在回答有点晚了,但无论如何.. 您不能将 istringstream 作为接收缓冲区传递。 Boost 消息队列只处理原始字节,不处理类似 std 的对象。

要使其工作,您必须使用 char 数组或之前使用 malloc 保留的任何缓冲区。

例如:

char buffer [1024];
mq.receive(buffer, sizeof(buffer), recvd_size, pri);

发送也是一样,只能发送原始字节,所以不能使用ostringstream。

希望对你有帮助。

【讨论】:

  • 是的,这就是答案,我忘了自动回复这个问题谢谢:)
猜你喜欢
  • 2015-07-22
  • 2012-09-24
  • 2013-07-09
  • 2011-09-07
  • 2013-08-25
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
相关资源
最近更新 更多