PDA

View Full Version : c++ newb - STL vector problems


Ian Jones
01-20-2007, 05:53 AM
Hi there, I'm a newbie to c++ and having trouble with a particle system I'm attempting to write. I'm really inexperienced with pointers and memory allocation, aswell as STL so I am probably making a really simple error. I'm getting crashes after running my code for 30 seconds or so. Here's a stripped down example of where my problem lies.


static void renderGL(std::vector<Particle *> & pVec) {

//--- Add new particle every frame, limit to 1000 ---
if(pVec.size() < 1000) {
Particle *p = new Particle(720, 450);
pVec.push_back(p);
}

std::vector<Particle *>::iterator iter;

//--- Iterate through vector and call particle class member function update() ---
for(iter = pVec.begin(); iter != pVec.end(); iter++) {
(*iter)->update();
}

//--- Iterate through vector, check particle class member function getLife() ---
//--- Delete particle from memory, then remove erase() from vector ---

for(iter = pVec.begin(); iter != pVec.end();) {
if((*iter)->getLife() == 0) {
delete (*iter);
iter = pVec.erase(iter);
} else {
++iter;
}
}

}


static void eventLoop() {

//--- Add 10 particles to vector begin with ---
std::vector<Particle *> pVec;
for(int i=0; i<10; i++) {
Particle *p = new Particle(720, 450);
pVec.push_back(p);
}

//--- Loop until exit ---
int done = 0;
while(!done) {
renderGL(pVec);
}

}


Basically I get a bad allocation error and signal 9 sigbrt which kills the application sometimes or hangs the entire system (OsX Tiger). I think I'm not correctly freeing the memory of a created Particle. I think I'm erasing the vector entry correctly though. This isn't all the code, I'm happy to provide the source if it helps at all... I'd really appreciate any help, atm this is a big stumbling block I'm eager to get past and understand what's going on.

T-bat
01-20-2007, 04:22 PM
I thought it could be this line:

iter = pVec.erase(iter);

but that wouldn't probably cause crash after 30 seconds..

Nevermind. Try changing that line to:

pVec.erase(iter);

Ian Jones
01-21-2007, 03:29 AM
It's actually closer to about a minute that it takes to crash. Changing as you suggested doesn't fix the problem, the code still runs though. The reason I'm assigning the result of the erase to the iterator is because according to this: http://www.fredosaurus.com/notes-cpp/stl-containers/vector/header-vector.html

'Removes element at iter position and returns position of next element'

Hence my little if else controlling the incrementation of the iteration.

Ian Jones
01-21-2007, 08:18 AM
Ok, I seem too have fixed my problem... it appears that I am using the vector properly and memory is being freed correctly. Because I was just implementing this I thought that this was the problem but it turns out I had a memory leak elsewhere into my VRAM not the heap. So the bug was unrelated to the vector. :)

CGTalk Moderation
01-21-2007, 08:18 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.