Memory Leak?

Started by
10 comments, last by Jansic 11 years, 9 months ago
Hey, I am currently working through Beginning DirectX 9 by Wendy Jones. I am currently on a chapter which shows you how to create a particle system.

I think I am getting to grips with it, but at the moment, I am trying to practice writing the code from notes. I've got the code to run, with a few errors which I've managed to fix.

The part which is bugging me at the moment is when I debug the code and the program runs, the particles spray into which ever direction and when I close the window, a dialog box appears which says:


Debug Assertion Failed!

Program: ... [insert directory path]
File:c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector
Line 932

Expression: vector subscript out of range
[/quote]

Now when I had a look at the code that came on the CD, there was a part of the code which was trying to erase an object pointer of the Emitter class, but using the standard vector library. Now, I am guessing you can't call std::vector functions and erase the elements if it isn't of type std::vector.

Here is the code that is trying to remove the emitters:


void ParticleManager::removeEmitter(int emitterNum)
{
iterateEmitter(emitter[emitterNum]);
}
void ParticleManager::iterateEmitter(Emitter* which)
{
// emitter.erase(&which);

std::vector <Emitter*>::iterator Iter;
for(Iter = emitter.begin(); Iter != emitter.end(); Iter++)
{
if(*Iter != which)
{
emitter.erase(Iter);
break;
}
}
}


The emitter object is of type std::vector. It's the part of the code that's commented. If I use that which was in the provided code, will have a red mark underneath the dot and the last bracket. I am just stuck at what to do, I don't think I have found a good book that really explains how the std::vector works, so this is most likely why I have run into problems like this because I've not learnt it in detail...
Advertisement
Never mind, I've managed to fix it. I found a site that mentions using push_back to add a new element to the end of the vector.

Unless this is the wrong way of doing. I've put:

emitter.push_back(which);

where the comment was, so the erase isn't there anymore. I'm just wondering, because I am adding a new element at the end, the memory isn't actually deleted...
You replaced a call to erase with a call to push_back and it worked? What were you trying to do? The function names are not helping me here. Why does a method called iterateEmitter delete or add anything?
I decided to change some of the names so I know which functions I am calling. Like I said, I've not had a good book explain how to use std::vector and all of it's library. I also find the documentation to be useless without knowing how it works properly.

I will post the original code from the CD:


void particleManager::removeEmitter(int emitterNum)
{
removeEmitter(emitters[emitterNum]);
}

void particleManager::removeEmitter(Emitter *which)
{
emitters.erase(&which);

std::vector <Emitter*>::iterator Iter;

for ( Iter = emitters.begin( ) ; Iter != emitters.end( ) ; Iter++ )
{
if (*Iter == which)
{
emitters.erase(Iter);
break;
}
}
}
That code seems wrong to me. What is `emitters.erase(&which);' supposed to do? The method `erase' takes iterators, but `&which' is the address of a parameter, which makes no sense.

Also, if you have the index of the element you want to remove (like in the first method), it seems completely backwards to get its content and then go around finding it...
It's what was provided on the CD, so if you're saying it's wrong, I don't have a clue what the person was thinking when writing up the code.

It's just confusing me now. I've been trying to sort this code out for a while now and finally managed to get it running, but with this stupid debug assertion thing.

I'm not sure how these two functions work properly. I know that the first function is called twice with an index of 0 and 1 which indicates which emitters are to be erased. There are only two emitters, so the size would only two? The first function calls the second function and loops through the elements of the two created emitters and erases them.

That's my take on it, I'm not too sure though. The book doesn't even mention these functions at all and there weren't any comments provided with the CD, so I was pretty much left in the dark on this one...
That can be done more easily


void particleManager::removeEmitter(int emitterNum)
{
emitters.erase(emitters.begin() + emitterNum);
}

void particleManager::removeEmitter(Emitter *which)
{
std::vector<Emitter*>::iterator Iter = std::find(emitters.begin(), emitters.end(), which);
emitters.erase(Iter);
}
Ah, looking at Codarki's code is such a relief. My eyes were bleeding from the other code. :)
Ok, I've just tried your code Codarki, but I am getting a different Debug Assertion, but on line 1169. It says:

Expression: vector erase iterator out of range.

The only way I could fix this is by getting rid of "+ emitterNum" in the first call to erase, but that would result in me not erasing the second emitter.

Also, I'm not too sure what I am meant to pass through when I call the second function.


Anyway, wouldn't it be easier to just call the clear function to clear all of the elements in the vector? I tried it and I'm not getting the debug assertion.
emitters vector holds pointers. simple erase or clear would not destroy particles.

you should call delete on them.

This topic is closed to new replies.

Advertisement