簡體   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