繁体   English   中英

在Boost中访问顶点时出现分段错误

[英]Segmentation fault while accessing vertex in boost

我有以下代码:

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);
}
}
};

现在,我在print_things中遇到了分段错误。 如果我只打印最后一个initial_state的state_name ...那么它就可以正常工作....但是如果我尝试从第一个初始状态打印,则会出现分段错误。 为什么会这样

我认为变量state_name是外部for循环的局部变量。 退出循环后,变量将被销毁,并且将对释放内存的引用放入向量中。 如果我正确地阅读了代码,则应在退出循环之前将state_name插入向量中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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