Jump to content
  • Advertisement
Sign in to follow this  
nluk

access violation when using vectors

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello I have a opengl canvas class, which contains a vector that holds a list of shape objects that gets displayed. Anyways the problem is when I try to remove the shape I get an access violation void GLcanvas::addShape(GLshape* x) { theDisplayLists.push_back(x); //will be used to remove object theDisplayLists[numInList]->position = theDisplayLists.begin(); numInList++; } //where x will be (shape class)->position void GLcanvas::removeShape(GLshape** x) { theDisplayLists.erase(x); //access violation numInList--; } I'm using borland builder 6

Share this post


Link to post
Share on other sites
Advertisement
Not sure if you noticed it but you added a pointer to the shape to the list and you're trying to remove a "pointer to a pointer" from the list... That's whats causing your access violation.

Share this post


Link to post
Share on other sites
Ok, first off, and this doesn't have anything to do with your problem, the numInList variable is pointless, since std::vector give a size() member function that will tell you how many elements are in the vector.

As for your actual problem, you're probably supplying an invalid value for your element address that isn't in the actual vector. Consider instead using the value of the GLshape pointer and using the remove-erase idiom to remove the item. Ex:

void GLcanvas::removeShape(GLshape* x)
{
theDisplayLists.erase(std::remove(theDisplayLists.begin(), theDisplayLists.end(), x),
theDisplayLists.end());
}

Share this post


Link to post
Share on other sites
Why do you keep track of the number of elements with your own variable? std::vector proviced the size() member function.

Also you pass a GLShape* to the add function and a GLShade** to the remove function.

Also theDisplayLists[numInList]->position = theDisplayLists.begin(); smells. It possibly accesses the vector at a out of range position. And you should not store iterators to your objects anywhere since they can become invalidated if you add new elements to the vector.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!