简体   繁体   中英

Segmentation fault while accessing vertex in boost

i have the following code:

class Translator
{
typedef property<symbol_t,string,property <message_t,string> > edge_properties;
typedef property<vertex_name_t,string, property<vertex_index_t,int,property<vertex_index1_t,bool> > > vertex_properties;
typedef adjacency_list<listS,listS,directedS,vertex_properties,edge_properties> Graph;
typedef property_map<Graph,vertex_name_t> :: type name_id ;
typedef graph_traits<Graph>::vertex_descriptor vert_descript;
vector<vert_descript> Process_initial_state;
vector<Graph> Processes;
vector<name_id> Process_state_name;

Translator(const char *path)
{
    generate_automata(path);
    print_things();
}

void print_things()
{
    vert_descript vert;
    for(int i=0;i<Process_initial_state.size();i++)
    cout<<endl<<Process_state_name[i][Process_initial_state[i]];
}
    void generate_automata(const char *path)
{

    xml_document xml;
    xml_parse_result xml_result = xml.load_file(path);
    xml_node temp = xml.first_child();
    graph_traits<Graph>::vertex_descriptor vertex;
    graph_traits<Graph>::vertex_descriptor from_vertex;
    graph_traits<Graph>::vertex_descriptor to_vertex;
for(temp = temp.child("role");temp;temp = temp.next_sibling("role"))
    {
        //Adding states to the Graph
        string role = temp.attribute("name").value();
        Graph process;
        name_id state_name = get(vertex_name,process);
        vert_descript istate;
        vector<vert_descript > fstates;
        for(xml_node temp1 = temp.child("states").child("state");temp1;temp1 = temp1.next_sibling())
        {
            string state = temp1.child_value();
            state = role + "_" + state;
            vertex = add_vertex(process);
            state_name[vertex] = state;
            if(temp1.attribute("type") &&  (!strcmp(temp1.attribute("type").value(),"initial")))
            {
            istate = vertex;
            }
            if(temp1.attribute("type") && (!strcmp(temp1.attribute("type").value(),"final")))
            fstates.push_back(vertex);
        }
    }
Process_initial_state.push_back(istate);
Process_state_name.push_back(state_name);
}
}
};

Now, i am getting a segmentation fault in print_things. If i simply print the state_name of last initial_state...then it works fine....but if i try to print from the 1st initial state then i get a segmentation fault. Why's this happening..

I think that the variable state_name is local to the outer for loop. After you exit the loop the variable is destroyed and you are putting a reference to deallocated memory into your vector. If I am reading your code correctly, you should push state_name into your vector before you exit the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM