复制包含 BGL 图的 class 时无限循环
Infinite loop when copying a class containing a BGL graph
我有一个结构(状态是一个纯虚拟抽象class):
using GraphColorGraph = boost::adjacency_list<boost::listS, boost::setS, boost::undirectedS,
boost::property<boost::vertex_index_t, size_t>>;
using GraphColorEdge = boost::graph_traits<GraphColorGraph>::edge_descriptor;
using GraphColorVertex = boost::graph_traits<GraphColorGraph>::vertex_descriptor;
class GraphColor : public State<ZykovMove> {
private:
GraphColorGraph graph;
public:
GraphColor() = default;
explicit GraphColor(const GraphColorGraph &graph) : graph(graph) {}
/*GraphColor(const GraphColor &other): State(other) {
boost::copy_graph(other.graph, this->graph);
}
GraphColor &operator=(const GraphColor &other) {
boost::copy_graph(other.graph, this->graph);
return *this;
}*/
}
问题,我有一个函数注定会被调用多次(很多次!):
std::pair<GraphColor, Vertex> expand(const Vertex ¬_important, const GraphColor& root , Graph ¬_important) const {
// ... code
auto copy_root = root; // infinite loop if copy contructors enabled
return make_pair(copy_root, foo) // seg fault if the copy constructors not enabled.
}
我可能在代码中隐藏了未定义的行为,因为当第二次调用此函数时,复制会导致无限循环。但是如果在 GraphColor
中禁用复制构造函数,则复制 GraphColor
没有问题,但在 make_pair() 中存在问题,它会产生由 InvalidRead (valgrind) 引起的段错误。在这两个调用中 root
没有改变。
已解决,我有未定义的行为,因为我正在存储 vertex_descriptor 并试图在另一个图表中使用它导致这种效果,这些 vertex_descriptor 无效。
我有一个结构(状态是一个纯虚拟抽象class):
using GraphColorGraph = boost::adjacency_list<boost::listS, boost::setS, boost::undirectedS,
boost::property<boost::vertex_index_t, size_t>>;
using GraphColorEdge = boost::graph_traits<GraphColorGraph>::edge_descriptor;
using GraphColorVertex = boost::graph_traits<GraphColorGraph>::vertex_descriptor;
class GraphColor : public State<ZykovMove> {
private:
GraphColorGraph graph;
public:
GraphColor() = default;
explicit GraphColor(const GraphColorGraph &graph) : graph(graph) {}
/*GraphColor(const GraphColor &other): State(other) {
boost::copy_graph(other.graph, this->graph);
}
GraphColor &operator=(const GraphColor &other) {
boost::copy_graph(other.graph, this->graph);
return *this;
}*/
}
问题,我有一个函数注定会被调用多次(很多次!):
std::pair<GraphColor, Vertex> expand(const Vertex ¬_important, const GraphColor& root , Graph ¬_important) const {
// ... code
auto copy_root = root; // infinite loop if copy contructors enabled
return make_pair(copy_root, foo) // seg fault if the copy constructors not enabled.
}
我可能在代码中隐藏了未定义的行为,因为当第二次调用此函数时,复制会导致无限循环。但是如果在 GraphColor
中禁用复制构造函数,则复制 GraphColor
没有问题,但在 make_pair() 中存在问题,它会产生由 InvalidRead (valgrind) 引起的段错误。在这两个调用中 root
没有改变。
已解决,我有未定义的行为,因为我正在存储 vertex_descriptor 并试图在另一个图表中使用它导致这种效果,这些 vertex_descriptor 无效。