Jump to content
  • Advertisement
Sign in to follow this  
cood

STL Vectors freeze on push_back in release mode

This topic is 4905 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

In Debug mode my project works perfectly fine... but in release mode whenever I push_back() it may lock my program up for up to 10 seconds. Whats Debug mode have set different than release does when allocating memory? I just read that vectors store everything contiguously in memory too, is that true? cause that would be great to use for openGL vertex buffers then. I also read that sometimes on a push_back() they allocate much more memory than required for efficiency on later push_back()s. I see how this could make a pause like its doing, but why not in debug mode? Also, I did a reserve(numItems) when loading lots of items into a vector at one time (loading a saved file) and that solved the problem there. But when I need a single push_back for one item, i dont know that I should call this. Thanks a bunch for any help or suggestions. Here is my copy constructor, it should use the overloaded assignment operator: //COPY CONSTRUCTOR Poly::Poly(const Poly ©Poly) { *this = copyPoly; } //OVERLOADED ASSIGNMENT OPERATOR const Poly & Poly::operator=(const Poly ©Poly) { this->removeVertice(); //POLYGON for(int i = 0; i < copyPoly.numVertice; i++) vertice.push_back(copyPoly.vertice); normal = copyPoly.normal; flipNorm = copyPoly.flipNorm; midPoint = copyPoly.midPoint; numVertice = copyPoly.numVertice; strcpy(name, copyPoly.name); selectedVertex = -1; complete = copyPoly.complete; //POLYGON - TEXTURE if(copyPoly.texture) { for(int i = 0; i < copyPoly.numVertice; i++) { textureCoords.push_back(copyPoly.textureCoords); sTangents.push_back(copyPoly.sTangents); tTangents.push_back(copyPoly.tTangents); } } strcpy(textureDir, copyPoly.textureDir); strcpy(textureName, copyPoly.textureName); textureRotAngle = copyPoly.textureRotAngle; textureScale = copyPoly.textureScale; texture = copyPoly.texture; normalMap = copyPoly.normalMap; upNormalMap = copyPoly.upNormalMap; shareTexture = copyPoly.shareTexture; textureBits = copyPoly.textureBits; textureID = copyPoly.textureID; normalMapID = copyPoly.normalMapID; //cubeMapID; //this->setTexture(textureName); //POLYGON - COLOR colors = copyPoly.colors; specularColor = copyPoly.specularColor; shininess = copyPoly.shininess; //POLYGON - VOLUMETRIC FOG for(int i = 0; i < copyPoly.numVertice; i++) verticeFog.push_back(copyPoly.verticeFog); volumetricFog = copyPoly.volumetricFog; return *this; } //CREATE A NEW POLGON Poly tempPolygon; polygons.push_back(tempPolygon); numPolygons++; //CREATE A NEW VERTEX //ADD VERTICE TO POLYGON polygons[numPolygons - 1].addVertex(lParam, gridSize, verticalScroll, horizontalScroll, view, workingPosition); . . . //INSERT VERTEX vertice.push_back(tempVertex); numVertice++; selectedVertex = (int)vertice.size() - 1; [Edited by - cood on March 16, 2005 11:45:10 AM]

Share this post


Link to post
Share on other sites
Advertisement
Post your code. It is highly unlikely that push_back() is actually the culprit, its probably in the copy ct of your class or in how you are calling push_back().

Share this post


Link to post
Share on other sites
My first guess is that the problem is not the actual allocation, but the copying of the current items. You are correct that a vector increases its size significantly when it reallocates. Typically, it doubles its size whenever it's capacity isn't large enough for a new element. But this shouldn't be obscenely slow, especially in release mode. But every time it reallocates, it also has to copy all of the current elements from their original buffer to the newly allocated buffer, so for large vectors, a lot of copying is going to take place. If you have a non-trivial copy constructor (assuming your elements are class objects), then it is possible that release mode is doing something funky in there that debug wasn't doing.

One potential quick fix might be to .reserve() enough space for the vector from the very beginning, so that it won't ever have to do the reallocation/copy process. This might not be doable for your current situation, though; it depends on what you're using the vector for.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!