c++ - segmentation fault in iterator of std::list -
class declarations
struct cmp_str { bool operator()(char const *a, char const *b) { return std::strcmp(a, b) < 0; } }; class event_t { public: event_t(string *_session_time, string *_event_type, string *_table_name, string *_num_of_events); ~event_t(); char *table_name; char *event_type; pthread_mutex_t lock; pthread_cond_t cond; int num_of_events_threshold; double time_out; int num_of_events_so_far; }; std::map <char*, std::list<event_t*>, cmp_str > all_events; //global map
i have function waits variable reach threshold done through pthread_cond , pthread_mutex. returns. last line gives segmentation fault.
void foo(){ //don't worry event object ic created event_t *new_event = new event_t(args[0]->val_str(s),(args[1]->val_str(s)), (args[2]->val_str(s)), (args[3]->val_str(s))); map<char*, list<event_t*> >::iterator map_it; map_it = all_events.find(new_event->table_name); if(map_it == all_events.end()){ list<event_t*> *my_list = new list<event_t*>(); my_list->push_back(new_event); all_events[new_event->table_name] = *my_list; } else{ map_it->second.push_back(new_event); (list<event_t*>::iterator list_it=map_it->second.begin(); list_it!=map_it->second.end(); ++list_it) std::cout << (*list_it)->event_type << " " << (*list_it)->time_out << " " << (*list_it)->num_of_events_threshold << '\n'; } /* * waiting number of events reach threshold. */ pthread_mutex_lock(&new_event->lock); while(new_event->num_of_events_so_far < new_event->num_of_events_threshold ) pthread_cond_wait(&new_event->cond, &new_event->lock); pthread_mutex_unlock(&new_event->lock); // segmentation fault!!! (list<event_t*>::iterator list_it=map_it->second.begin(); list_it!=map_it->second.end(); ++list_it) std::cout << (*list_it)->event_type << " " << (*list_it)->time_out << " " << (*list_it)->num_of_events_threshold << '\n'; }
if(map_it == all_events.end()){ list<event_t*> *my_list = new list<event_t*>(); my_list->push_back(new_event); all_events[new_event->table_name] = *my_list; }
this code doesn't leave map_it
pointing valid iterator, when dereference @ bottom of function crash.
you want add line like
map_it = all_events.find(new_event->table_name);
inside if { }
clause.
Comments
Post a Comment