【问题标题】:Microsoft C++ exception: boost::archive::archive_exception at memory location(Parsing Vector through client server Boost Udp)Microsoft C++ 异常:内存位置的 boost::archive::archive_exception(通过客户端服务器 Boost Udp 解析向量)
【发布时间】:2023-04-07 12:50:01
【问题描述】:

exception: boost::archive::archive_exception at memory location

我发现这个但没用,因为他是为文件做的,而我只想为矢量做。

问题:我在服务器端对向量进行了序列化并解析到客户端,然后我正在反序列化。所以 1/2 记录一切正常。

但如果我插入超过 2 条记录并通过向量解析它,它会在我反序列化数据的客户端出现此错误:Microsoft C++ 异常:内存位置的 boost::archive::archive_exception

这是我的代码:

服务器端:

通过将向量作为输入来序列化数据

//I think these headers are enough !!!

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include<boost/serialization/vector.hpp>
#include<boost/serialization/map.hpp>

template<class input>
void UdpServer::serializeData(input &vector) { // Here i use vector in parameter
    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << vector;
    sendMessageToClients(archive_stream.str());//sending to client
}

我得到错误的客户端,

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include<boost/serialization/vector.hpp>
#include<boost/serialization/map.hpp>

//this is what i received from the server
size_t const len = m_socket.receive_from(
        boost::asio::buffer(recv_buf), m_sender_endpoint);

    std::string const received_message(recv_buf.data(), len);

std::string archive_data(received_message);// receive from server
        std::istringstream archive_stream(archive_data);
        boost::archive::text_iarchive archive(archive_stream);
        archive >> map;
        displayCarData(map); //i'm passing this to function for printing the values   

我尝试添加 Boost io 二进制库,但错误是: c ++超出范围内存位置 //这是我使用io二进制库的尝试

 std::string archive_data(received_message);
            std::istringstream archive_stream(archive_data);
            boost::archive::text_iarchive archive(archive_stream, boost::io::binary);
            archive >> map;
            displayCarData(map);

任何帮助将不胜感激,

提前致谢,

【问题讨论】:

  • 缺少minimal reproducible example。这里有太多未知的类型和变量。 Edit你的问题包含足够的代码,我们可以编译它。
  • 会不会是序列化的数据超过了包大小
  • 我认为这是可能的@sehe 但是有没有办法设置它的大小???
  • @1201ProgramAlarm 哪些变量你不明白?
  • serializeData 中使用的类inputarchive_data 中使用的map 的类和类型。

标签: c++ memory serialization boost deserialization


【解决方案1】:
//this is what i received from the server
size_t const len = m_socket.receive_from(
    boost::asio::buffer(recv_buf,1024), m_sender_endpoint);

我认为你必须在

中给出尺寸
 boost::asio::buffer(recv_buf,1024), m_sender_endpoint);

在这一行中,也适用于服务器端。

【讨论】:

    【解决方案2】:
    archive << vector;
    

    建议您确实序列化std::vector

    archive >> map;
    

    建议您反序列化 std::map

    那是行不通的。实际上很可能是Undefined Behaviour

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 1970-01-01
      • 2016-05-31
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      相关资源
      最近更新 更多