c++ - Vector of pointers segfaulting on call to size() and upon creation of an iterator -
i have class contains vector of pointers class. in of member functions, can access , modify vector fine. however, in 1 function calling .size() or getting iterator causes segfault in program.
here class vector of pointers class:
class floor { public: floor(string imagefile, string a_walkwayfile, string waypointfile, int a_id, sf::vector2i a_pos); ~floor(); int getid() { return id; }; void update(sf::renderwindow* app); sf::sprite* getsprite() { return sprite; }; vector<int> parsecsvline(string line); void loadwalkways(); void converttoview(sf::renderwindow* app, sf::view* view); bool pointinwalkway(sf::vector2f pt); private: vector<sf::rectangleshape*> walkways; sf::sprite* sprite; int id; float scale; string walkwayfile; };
here snippet whee objects added vector:
walkways.push_back(new sf::rectangleshape()); walkways[walkways.size() - 1]->setposition(sf::vector2f(parsedline[0], parsedline[1])); walkways[walkways.size() - 1]->setsize(sf::vector2f(parsedline[2] - parsedline[0], parsedline[3] - parsedline[1])); walkways[walkways.size() - 1]->setfillcolor(sf::color(0,0,255,25)); walkways[walkways.size() - 1]->setoutlinecolor(sf::color::blue); walkways[walkways.size() - 1]->setoutlinethickness(3);
here snippet of code accesses vector fine always:
void floor::update(sf::renderwindow* app) { app->draw(*sprite); for(int = 0; < walkways.size(); ++i) app->draw(*walkways[i]); }
now here function every call or reference function results in segfault:
bool floor::pointinwalkway(sf::vector2f pt) { cout << walkways.size() << endl; // segfaults if(walkways.size() != 0) // segfaults for(vector<sf::rectangleshape*>::iterator = walkways.begin(); != walkways.end(); ++it) // segfaults too! if((*it)->getglobalbounds().contains(pt.x, pt.y)) return true; return false; }
any pointers how fix appreciated!
your file loop reads 1 many times, try using loop structure:
string line; getline(file, line); while(file) { if(parsedline.size() == 4) { walkways.push_back(new sf::rectangleshape()); walkways[walkways.size() - 1]->setposition(sf::vector2f(parsedline[0], parsedline[1])); walkways[walkways.size() - 1]->setsize(sf::vector2f(parsedline[2] - parsedline[0], parsedline[3] - parsedline[1])); walkways[walkways.size() - 1]->setfillcolor(sf::color(0,0,255,25)); walkways[walkways.size() - 1]->setoutlinecolor(sf::color::blue); walkways[walkways.size() - 1]->setoutlinethickness(3); /*sf::rectangleshape* rect = new sf::rectangleshape(); rect->setposition(sf::vector2f(parsedline[0], parsedline[1])); rect->setsize(sf::vector2f((float)parsedline[2] - parsedline[0], (float)parsedline[3] - parsedline[1])); rect->setfillcolor(sf::color(0,0,0,0)); //rect->setorigin(sf::vector2f(parsedline[0], parsedline[2])); //cout << parsedline[0] << ", " << parsedline[1] << endl; rect->setoutlinecolor(sf::color::blue); rect->setoutlinethickness(3); walkways.push_back(rect);*/ } getline(file, line); }
this prevents file reading 1 many times , stop correctly.
Comments
Post a Comment