c++ - Vector of shared_ptr gives seg fault when instances in vector accessed -


i have vector of shared_ptr<someclass> named allparts.

the code below:

void function thisiswhereitstarts(){     vector<shared_ptr<someclass> > allparts;     for(i=0;i<n;i++){         allparts.push_back(function_which_returns_shared_ptr_someclass());     }  // use vector below:       for(vector<shared_ptr<someclass> >::iterator = allparts.begin(); it!=allparts.end(); it++){          (*it)->function_of_someclass() ; // gives segmentation fault     } } 

i've used vector of pointers number of times before, first time i'm using shared_ptr .

the function returns shared_ptr this:

shared_ptr<someclass> function_which_returns_shared_ptr_someclass(){      shared_ptr<someclass> part(new someclass);     if(part->some_function(some_parameter)){          return part;     }else{          return shared_ptr<someclass>();     }  }  

you push_back empty shared_ptr. dereference every shared_ptr in vector. dereferencing empty shared_ptr fail. either don't push_back empty pointers or don't dereference them.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -