C++ C++;引导序列化会导致分段错误
我有这两个类,我正在尝试使用boost反序列化它们C++ C++;引导序列化会导致分段错误,c++,c++11,boost,boost-serialization,C++,C++11,Boost,Boost Serialization,我有这两个类,我正在尝试使用boost反序列化它们 class WorldItem { private: friend class boost::serialization::access; template <class Archive> void serialize(Archive &ar, const unsigned int version) { ar &foreground; ar &b
class WorldItem
{
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &foreground;
ar &background;
ar &breakLevel;
ar &breakTime;
ar &water;
ar &fire;
ar &glue;
ar &red;
ar &green;
ar &blue;
}
public:
int foreground = 0;
int background = 0;
int breakLevel = 0;
long long int breakTime = 0;
bool water = false;
bool fire = false;
bool glue = false;
bool red = false;
bool green = false;
bool blue = false;
};
class Worlds
{
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &width;
ar &height;
ar &name;
for (int i = 0; i < 100 * 60; i++)
{
ar &items[i];
}
ar &owner;
ar &weather;
ar &isPublic;
ar &isNuked;
}
public:
int width;
int height;
string name;
WorldItem *items;
string owner = "";
int weather = 0;
bool isPublic = false;
bool isNuked = false;
};
这里我用这个函数来序列化世界
std::stringstream serialize_world(Worlds world)
{
std::stringstream str;
{
boost::archive::binary_oarchive oa(str);
oa << world;
}
return str;
}
我得到了一个分割错误(核心转储),我不知道是什么问题
谢谢。看起来您永远不会从
generateWorld
返回值
我的编译器对此发出警告。尝试启用编译器的诊断。我通常启用了-Wall-Wextra-pedantic
此外,在反序列化
中,您从不将项
初始化为任何内容。这将导致一场灾难
大多数编译器也可以对此进行诊断(-fsanize=address,undefined
有帮助,尽管这会降低编译和运行时的速度)。还有像Valgrind这样的外部工具可以实现这些功能
最后,我不知道blobdata发生了什么,所以我将忽略它,但它看起来也错了
不使用原始新建/删除
另见例如
只需使用std::array,然后就可以开心了:
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <iostream>
#include <sstream>
class WorldItem {
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& foreground& background& breakLevel& breakTime& water& fire& glue&
red& green& blue;
}
public:
int foreground = 0;
int background = 0;
int breakLevel = 0;
long long int breakTime = 0;
bool water = false;
bool fire = false;
bool glue = false;
bool red = false;
bool green = false;
bool blue = false;
auto operator<=>(WorldItem const&) const = default;
};
class Worlds
{
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& width& height& name& items& owner& weather& isPublic& isNuked;
}
public:
int width;
int height;
std::string name;
std::array<WorldItem, 100 * 60> items;
std::string owner = "";
int weather = 0;
bool isPublic = false;
bool isNuked = false;
auto operator<=>(Worlds const&) const = default;
};
//So here im creating a world in this way
Worlds generateWorld(std::string name, int width, int height) {
Worlds world;
world.name = name;
world.width = width;
world.height = height;
return world;
}
std::string serialize_world(Worlds const& world) {
std::stringstream str;
{
boost::archive::binary_oarchive oa(str);
oa << world;
}
return str.str();
}
Worlds deserialize(std::string world) {
Worlds wld;
std::istringstream blobdata(world);
{
boost::archive::binary_iarchive ia(blobdata);
ia >> wld;
}
return wld;
}
int main() {
Worlds w = generateWorld("test", 6, 6);
Worlds clone = deserialize(serialize_world(w));
std::cout << "Worlds equal? " << std::boolalpha << (w == clone) << "\n";
}
我使用了你发送给我的std::array实际上帮助了我,但由于某种原因,我仍然收到了这个错误,在抛出“boost::archive::archive\u exception”what()的实例后调用了terminate:输入流错误中止(内核转储)在反序列化更新之后,它起了作用。非常感谢你对我的帮助,我真的很感激it@cmdocmd干杯欢迎光临。也请考虑解决问题的答案。添加了一些关于如何自己查找此类问题的注释
Worlds deserialize(std::string world)
{
Worlds wld;
std::istream *blobdata = WORLD_DATA(world);
{
boost::archive::binary_iarchive ia(*blobdata);
ia >> wld;
}
return wld;
}
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <iostream>
#include <sstream>
class WorldItem {
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& foreground& background& breakLevel& breakTime& water& fire& glue&
red& green& blue;
}
public:
int foreground = 0;
int background = 0;
int breakLevel = 0;
long long int breakTime = 0;
bool water = false;
bool fire = false;
bool glue = false;
bool red = false;
bool green = false;
bool blue = false;
auto operator<=>(WorldItem const&) const = default;
};
class Worlds
{
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& width& height& name& items& owner& weather& isPublic& isNuked;
}
public:
int width;
int height;
std::string name;
std::array<WorldItem, 100 * 60> items;
std::string owner = "";
int weather = 0;
bool isPublic = false;
bool isNuked = false;
auto operator<=>(Worlds const&) const = default;
};
//So here im creating a world in this way
Worlds generateWorld(std::string name, int width, int height) {
Worlds world;
world.name = name;
world.width = width;
world.height = height;
return world;
}
std::string serialize_world(Worlds const& world) {
std::stringstream str;
{
boost::archive::binary_oarchive oa(str);
oa << world;
}
return str.str();
}
Worlds deserialize(std::string world) {
Worlds wld;
std::istringstream blobdata(world);
{
boost::archive::binary_iarchive ia(blobdata);
ia >> wld;
}
return wld;
}
int main() {
Worlds w = generateWorld("test", 6, 6);
Worlds clone = deserialize(serialize_world(w));
std::cout << "Worlds equal? " << std::boolalpha << (w == clone) << "\n";
}
Worlds equal? true