[英]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. 现在,我在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.
如果我只打印最后一个initial_state的state_name ...那么它就可以正常工作....但是如果我尝试从第一个初始状态打印,则会出现分段错误。 Why's this happening..
为什么会这样
I think that the variable state_name
is local to the outer for
loop. 我认为变量
state_name
是外部for
循环的局部变量。 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. 如果我正确地阅读了代码,则应在退出循环之前将
state_name
插入向量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.