我将代码的基本代码块和它们之间的控制流表示为循环图,并希望能够将图形序列化为XML或类似代码,然后对其进行反序列化并在另一个应用程序中使用该图形。在尝试序列化数据结构时,我得到了数千次对序列化函数的调用,即使我用来测试应用程序的图形包含的节点少于50个,这让我相信循环引用存在问题。
当我运行或调试应用程序时,它会反复成功进入void serialize(Archive&, std::vector<BasicBlock*>&, const unsigned)
函数,直到它因分段错误而崩溃。输出文件已创建,但没有写入任何内容。
以下是我用来表示基本块的数据结构;
struct __attribute__ ((visibility ("default"))) BasicBlock {
unsigned int id;
unsigned int start_address;
unsigned int end_address;
std::vector<BasicBlock*>* outgoing;
std::vector<BasicBlock*>* incoming;
};
这是序列化类和函数
class Serialize {
public:
static void marshal(const std::vector<BasicBlock*>& basic_blocks, std::string filename) {
std::ofstream ofstream(filename.c_str(), std::ios::out | std::ios::binary);
boost::archive::text_oarchive archive(ofstream);
archive << basic_blocks;
}
static void unmarshal(const std::string filename, std::vector<BasicBlock*>& basic_blocks) {
std::ifstream ifstream(filename.c_str());
boost::archive::text_iarchive archive(ifstream);
archive >> basic_blocks;
}
};
namespace boost {
namespace serialization {
template<class Archive>
void save(Archive & archive, const BasicBlock& basic_block, const unsigned int version) {
archive & basic_block.id;
archive & basic_block.start_address;
archive & basic_block.end_address;
archive & basic_block.incoming;
archive & basic_block.outgoing;
}
template<class Archive>
void load(Archive & archive, BasicBlock& basic_block, const unsigned int version) {
archive & basic_block.id;
archive & basic_block.start_address;
archive & basic_block.end_address;
archive & basic_block.incoming;
archive & basic_block.outgoing;
}
template<typename Archive>
void serialize(Archive& archive, std::vector<BasicBlock>& basic_blocks, const unsigned version) {
archive & basic_blocks;
}
template<typename Archive>
void serialize(Archive& archive, std::vector<BasicBlock*>& basic_blocks, const unsigned version) {
archive & basic_blocks; // Triggered thousands of times...
}
}
}
BOOST_SERIALIZATION_SPLIT_FREE(BasicBlock)
答案 0 :(得分:1)
我的第一个怀疑是,vector
的'保存'重载实际上毁了你的东西。
template<typename Archive>
void serialize(Archive& archive, std::vector<BasicBlock*>& basic_blocks, const unsigned version) {
archive & basic_blocks; // Triggered thousands of times...
}
会......(间接)递归地称自己为AFAICT
您是否尝试包含boost/serialization/vector.hpp
?
这是一个显示其有效的SSCCE:看到它 Live on Coliru
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include <fstream>
struct BasicBlock {
unsigned int id;
unsigned int start_address;
unsigned int end_address;
std::vector<BasicBlock*>* outgoing;
std::vector<BasicBlock*>* incoming;
};
// BOOST_CLASS_TRACKING(BasicBlock, boost::serialization::track_always)
namespace boost {
namespace serialization {
template<class Archive>
void save(Archive & archive, const BasicBlock& basic_block, const unsigned int version) {
archive & basic_block.id;
archive & basic_block.start_address;
archive & basic_block.end_address;
archive & basic_block.incoming;
archive & basic_block.outgoing;
}
template<class Archive>
void load(Archive & archive, BasicBlock& basic_block, const unsigned int version) {
archive & basic_block.id;
archive & basic_block.start_address;
archive & basic_block.end_address;
archive & basic_block.incoming;
archive & basic_block.outgoing;
}
}
}
BOOST_SERIALIZATION_SPLIT_FREE(BasicBlock)
int main()
{
BasicBlock root
{
1, 2, 3,
new std::vector<BasicBlock*> { new BasicBlock { 4, 5, 6, nullptr, nullptr } },
new std::vector<BasicBlock*> { &root } // cyclic!
};
boost::archive::text_oarchive ar(std::cout);
ar << root;
// everything leaked :)
}