Frage

i'm making some test with boost interprocess and ptree structure, i have a segfault when i try to read the message sent(or when i try to parse it in json).

i'm using boost1.49 on debian linux.

i'm serializing it in json for later uses, and because i didn't find any good doc for the direct serialization of the boost property threes.

this is the code i'm using to test(the commed say where the segfault is):

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);
}
War es hilfreich?

Lösung

I know it's a bit late to an answer right now, but anyway.. You can't pass an istringstream as a buffer for receive. Boost message queues only handle raw bytes and don't handle std like objects.

To make it work, you must use a char array or any buffer previously reserved with malloc.

Ex:

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

For sending it's the same, you can only send raw bytes, so you can't use ostringstream.

Hope it helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top