Search code examples
c++boostboost-graph

Infinite loop when copying a class containing a BGL graph


I have a struct (State is a pure virtual abstract 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;
      }*/
}

The problem, I have a function that is destined to be called multiple times (many times!):

std::pair<GraphColor, Vertex> expand(const Vertex &not_important, const GraphColor& root , Graph &not_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.
}

I have maybe an undefined behavior hide in the code because, when the second call of this function occurs the copy causes an infinite loop. but if disable copy contructors in GraphColor there is no problem copying the GraphColor but there is a problem in the make_pair() it produces segfault caused by an InvalidRead (valgrind). In the two calls root doesn't change.


Solution

  • Resolved, I had undefined behavior because I was storing vertex_descriptor and trying to use it in another graph causing this effect, these vertex_descriptors were invalid.