Search code examples
boostboost-graph

Removing vertex and adding it back again causes crash in boost::graph?


I'm using a labeled graph

typedef boost::labeled_graph<boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, Room, RoomEdge>,std::string> MyGraph;

with

struct LineSegment{
    LineSegment(){portalToRoom="";}
    LineSegment(QPointF startPos_, QPointF endPos_): startPos(startPos_), endPos(endPos_) { portalToRoom="";}
    QPointF startPos;
    QPointF endPos;
    QGraphicsLineItem* sceneItem;
    std::string type;
    std::string portalToRoom;
};



struct Room{
    Room(){centroid.setX(-1); centroid.setY(-1);}

    std::string category;
    std::string vertex_id;
    std::vector<LineSegment> roomLayout;
    QPointF centroid;
    QGraphicsRectItem* centroidSceneItem;
};

struct RoomEdge{
    std::string edge_id;
};

which lets me access vertices with string ids.

I add a vertex and set it's relevant fields this way:

MyVertex u = boost::add_vertex(id, g);
g[id].category = category; //crashes here
g[id].vertex_id = id; 

And remove them like so:

// First we remove all edges from this vertex
BGL_FORALL_VERTICES(v, g, MyGraph){
    if (QString(g.graph()[v].vertex_id.c_str()).compare(roomIdToBeRemoved) == 0){
        ve = v;
        BGL_FORALL_OUTEDGES(v, e, g, MyGraph) {
            ev.push_back(e);
        }
    }
}

foreach(MyEdge e, ev){
    remove_edge(e,g);
}

// Then we remove the vertex itself
remove_vertex(roomIdToBeRemoved.toStdString().c_str(),g);

This so far seems to work. The trouble is, if I want to add a removed vertex once again with the same id, I get the following:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)

while setting the fields of the newly added vertex (above commented "crashes here").

What might be the reason? I'm using boost functions for removing/adding things.


Solution

  • The crash is a result of a bug in the implementation of boost::labeled_graph. It's present both in Boost 1.54.0 and 1.55.0 (latest release). I reported it via Boost's Trac.

    I managed to write a simple test case that reveals the problem. Running it through valgrind proves that the issue exists. I also created a simple patch that fixes the problem for my setting of boost::adjacency_list (see the bug report for all these files).