Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 异常:boost::archive::archive\u内存位置异常_C++_Exception_Serialization_Boost_Deserialization - Fatal编程技术网

C++ 异常:boost::archive::archive\u内存位置异常

C++ 异常:boost::archive::archive\u内存位置异常,c++,exception,serialization,boost,deserialization,C++,Exception,Serialization,Boost,Deserialization,当我尝试反序列化二进制数据时,我得到以下结果: 异常:boost::archive::archive\u内存位置异常 写: std::ofstream ofs(savePath); boost::archive::binary_oarchive out_arch(ofs); out_arch << mData; ofs.close(); 当我使用text\u iarchive\text\u oarchive时,效果很好 序列化的数据结构mData是ColorMatrixmData

当我尝试反序列化二进制数据时,我得到以下结果:

异常:boost::archive::archive\u内存位置异常

写:

std::ofstream ofs(savePath);
boost::archive::binary_oarchive out_arch(ofs);
out_arch << mData;
ofs.close();
当我使用text\u iarchive\text\u oarchive时,效果很好

序列化的数据结构mData是
ColorMatrix
mData

#include <algorithm>
#include <memory>
#include <boost/serialization/vector.hpp>

template<class T, class A = std::allocator<T> >
struct ColorMatrix {
    typedef T value_type;
    typedef std::vector<value_type, A> Container;

    ColorMatrix() : _b(0) {}
    ColorMatrix(int a, int b, value_type const& initial = value_type())
        : _b(0)
    {
        resize(a, b, initial);
    }
    ColorMatrix(ColorMatrix const& other)
        : _data(other._data), _b(other._b)
    {}

    ColorMatrix& operator=(ColorMatrix copy) {
        swap(*this, copy);
        return *this;
    }

    bool empty() const { return _data.empty(); }
    void clear() { _data.clear(); _b = 0; }

    int dim_a() const { return _b ? _data.size() / _b : 0; }
    int dim_b() const { return _b; }

    value_type* operator[](int a) {
        return &_data[a * _b];
    }
    value_type const* operator[](int a) const {
        return &_data[a * _b];
    }

    void resize(int a, int b, value_type const& initial = value_type()) {
        if (a == 0) {
            b = 0;
        }
        _data.resize(a * b, initial);
        _b = b;
    }

    void copyTo(ColorMatrix<T, A> &other){

        int myA = dim_a();
        int myB = dim_b();
        int otherB = other.dim_b();

        for (int line = 0; line < myA; ++line){
            int myStart = line * myB;
            int myEnd = (line + 1) * myB;
            int otherStart = line*otherB;

            std::cout << "Line: " << line << " S1: " << myStart << " E1: " << myEnd << " S2: " << otherStart << std::endl;

            std::copy(_data.begin() + myStart,
                _data.begin() + myEnd,
                other._data.begin() + otherStart);
        }
    }

    friend void swap(ColorMatrix& a, ColorMatrix& b) {
        using std::swap;
        swap(a._data, b._data);
        swap(a._b, b._b);
    }


private:
    Container _data;
    int _b;

    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & _data;
        ar & _b;
    }
};
#包括
#包括
#包括
模板
结构颜色矩阵{
类型定义T值_类型;
向量容器;
ColorMatrix():_b(0){}
颜色矩阵(int a,int b,value_type const&initial=value_type())
:_b(0)
{
调整大小(a、b、首字母);
}
ColorMatrix(ColorMatrix常数和其他)
:_数据(其他._数据),_b(其他._b)
{}
ColorMatrix&运算符=(ColorMatrix副本){
掉期(*本,副本);
归还*这个;
}
bool empty()常量{return _data.empty();}
void clear(){_data.clear();_b=0;}
int dim_a()常量{return_b?_data.size()/_b:0;}
int dim_b()常量{return_b;}
值类型*运算符[](int a){
返回和_数据[a*_b];
}
值类型常量*运算符[](int a)常量{
返回和_数据[a*_b];
}
void resize(int a,int b,value\u type const&initial=value\u type()){
如果(a==0){
b=0;
}
_数据。调整大小(a*b,初始值);
_b=b;
}
无效复制(ColorMatrix和其他){
int myA=dim_a();
int myB=dim_b();
int otherB=other.dim_b();
对于(int line=0;line
  • 确保归档文件的生命周期已关闭,特别是不重叠

  • 文本存档工作的事实让我怀疑您是否正确地编写了二进制流。还要注意,在Boost序列化中,您可以而不是将多个存档安全地连接到同一个steam

我有另一个答案,详细说明了这种情况,以及它在这个网站的文本存档中是如何工作的

更新

查看代码后(谢谢!)我发现以下注意事项适用:

  • 事实上,在这个简单的示例中,您无法明确地管理归档对象的生命周期。我已经看到这会导致问题(在MSVC IIRC上)。您也可以在[SO]上找到它。因此,请编写:

    cout << "write data" << endl;
    {
        std::ofstream ofs("data.dat");
        boost::archive::binary_oarchive out_arch(ofs);
        //boost::archive::text_oarchive out_arch(ofs);
        out_arch << mData;
    }
    
    cout << "read data" << endl;
    {
        std::ifstream ifs("data.dat");
        if (!ifs) {
            cout << "read error!" << endl;
            return 1;
        }
    
        boost::archive::binary_iarchive in_arch(ifs);
        //boost::archive::text_iarchive in_arch(ifs);
        in_arch >> mData2;
    }
    
  • 我还建议改进
    ColorMatrix
    类中字段和参数的命名


    考虑提供AT。或者至少显示什么 MDATA是精确的。我写了一个小测试,但是它都是有效的。我不明白问题是什么:\thx提示。我认为数据中的问题。问题是什么?不清楚。测试数据都OK。显然数据序列化工作是不正确的。听起来很像。“谢谢你的提示,我将放弃它们”。你没有显示代码来表明你没有这些问题。那么,你检查过了吗?我已经用我的检查结果更新了答案(请参阅录制的Live Codeing会话:())。请参阅结果代码:现在我在尝试读取时出现错误:“std::bad_alloc at memory location”。添加了测试代码。
    #include <iostream>
    #include <vector>
    #include <math.h>
    #include <fstream>
    #include <map>
    #include <fstream>
    #include <boost/archive/binary_oarchive.hpp>
    #include <boost/archive/binary_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/serialization/map.hpp>
    #include "ColorMatrix.h"
    
    using namespace std;
    
    int main()
    {
        cout << "start program" << endl;
        ColorMatrix<std::map<int, float>> mData;
        ColorMatrix<std::map<int, float>> mData2;
    
        const int mSize = 200;
    
        mData.resize(mSize, mSize);
    
        cout << "init" << endl;
        for (int x = 0; x < mSize; x++){
            for (int y = 0; y < mSize; y++){
                if (y % 2 == 0){
                    mData[x][y][0] = 1.f;
                    mData[x][y][1] = 0.66666f;
                }
                else if (y % 3 == 0){
                    mData[x][y][0] = 1.f;
                    mData[x][y][1] = 0.1111111111f;
                    mData[x][y][3] = 0.44444444f;
                }
                else{
                    mData[x][y][0] = 1.f;
                }
    
            }
        }
    
        cout << "write data" << endl;
        std::ofstream ofs("data.dat");
        boost::archive::binary_oarchive out_arch(ofs);
        //boost::archive::text_oarchive out_arch(ofs);
        out_arch << mData;
        ofs.close();
    
        cout << "read data" << endl;
        std::ifstream ifs("data.dat");
        if (!ifs) {
            cout << "read error!" << endl;
            return 1;
        }
    
        boost::archive::binary_iarchive in_arch(ifs);
        //boost::archive::text_iarchive in_arch(ifs);
        in_arch >> mData2;
        cout << "complete" << endl;
        return 0;
    }
    
    cout << "write data" << endl;
    {
        std::ofstream ofs("data.dat");
        boost::archive::binary_oarchive out_arch(ofs);
        //boost::archive::text_oarchive out_arch(ofs);
        out_arch << mData;
    }
    
    cout << "read data" << endl;
    {
        std::ifstream ifs("data.dat");
        if (!ifs) {
            cout << "read error!" << endl;
            return 1;
        }
    
        boost::archive::binary_iarchive in_arch(ifs);
        //boost::archive::text_iarchive in_arch(ifs);
        in_arch >> mData2;
    }
    
    std::ofstream ofs("data.dat", std::ios::binary);
    // ...
    std::ifstream ifs("data.dat", std::ios::binary);