我在使用 boost::serialization 时遇到了一个问题,我的代码在 Linux 中运行良好,但在 Wind

I met a question when using boost::serialization, my code works fine in linux, but met a exception in windows

本文关键字:代码 Linux 我的 问题 Wind 但在 一个 运行 serialization boost 遇到      更新时间:2023-10-16

我正在尝试使用 boost 保存 ORB-SLAM 构建的地图,代码在 Linux 中运行良好,但是当我将其移动到 Windows 时,我在加载数据时遇到了异常。下面是我代码的一部分,这是一个大项目,所以我只显示相关的代码。

.h 中的一些代码

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
boost::serialization::split_member(ar, *this, version);
}
template<class Archive>
void save(Archive & ar, const unsigned int version) const;

template<class Archive>
void load(Archive & ar, const unsigned int version);   

CPP 简介

template<class Archive>
void Map::load(Archive & ar, const unsigned int version)
{
unsigned int test_data;
int nItems;
ar & nItems;
cout << "{INFO}mspMapPoints size = " << nItems << endl;
for (int i = 0; i < nItems; ++i) {
MapPoint* pMapPoint = new MapPoint();
ar & *pMapPoint;
mspMapPoints.insert(pMapPoint);
}
ar & nItems;
cout << "{INFO}mspKeyFrames size = " << nItems << endl;
for (int i = 0; i < nItems; ++i) {
KeyFrame* pKeyFrame = new KeyFrame;
ar & *pKeyFrame;
mspKeyFrames.insert(pKeyFrame);
}     

ar & nItems;
cout << "{INFO}mvpKeyFrameOrigins size = " << nItems << endl;
for (int i = 0; i < nItems; ++i) {             
KeyFrame* pKeyFrame = new KeyFrame;
ar & *pKeyFrame;
/* TODO : VerifyHere*/
mvpKeyFrameOrigins.push_back(*mspKeyFrames.begin());
}     
ar & const_cast<long unsigned int &> (mnMaxKFid);
ar & test_data;
if (test_data == TEST_DATA)
cout <<">>Map Loading Validated as True" << endl;
else
cout <<"ERROR Map Loading Validated as False: Got -" << test_data << " :( Check Load Save sequence" << endl;
}

调用函数如下:

void System::LoadMap(const string &filename)
{
{
std::ifstream is(filename,std::ios::binary);

boost::archive::binary_iarchive ia(is, boost::archive::no_header);
//ia >> mpKeyFrameDatabase;
ia >> mpMap;
}
cout << endl << filename <<" : Map Loaded!" << endl;

}
void System::SaveMap(const string &filename)
{
std::ofstream os(filename,std::ios::binary);
{
boost::archive::binary_oarchive oa(os, boost::archive::no_header);
//oa << mpKeyFrameDatabase;
oa << mpMap;
}
cout << endl << "Map saved to " << filename << endl;
}

在 Linux 中保存和加载都可以,但加载只会抛出 boost::archive::archive_exception 问题,没有告诉我任何细节。

您应该仅显示相关代码。特别是,它应该显示您的存档选择。另外,您从未告诉我们/what/不起作用,但我的猜测是您正在Windows侧加载保存在linux端的存档?

二进制存档格式不可移植。因此,请务必避免这种情况或使用尝试便携的直接替代品:https://github.com/mika-fischer/eos-portable-archive