• Create Account

## Memory Leak?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Zichu  Members

109
Like
0Likes
Like

Posted 12 July 2012 - 07:09 AM

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

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...

### #2Zichu  Members

109
Like
0Likes
Like

Posted 12 July 2012 - 07:16 AM

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...

### #3Álvaro  Members

20270
Like
0Likes
Like

Posted 12 July 2012 - 07:31 AM

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?

### #4Zichu  Members

109
Like
0Likes
Like

Posted 12 July 2012 - 07:35 AM

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;
}
}
}


### #5Álvaro  Members

20270
Like
0Likes
Like

Posted 12 July 2012 - 07:41 AM

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...

Edited by alvaro, 12 July 2012 - 07:42 AM.

### #6Zichu  Members

109
Like
0Likes
Like

Posted 12 July 2012 - 07:50 AM

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...

### #7Codarki  Members

462
Like
2Likes
Like

Posted 12 July 2012 - 08:16 AM

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);
}
`

### #8Álvaro  Members

20270
Like
1Likes
Like

Posted 12 July 2012 - 08:33 AM

Ah, looking at Codarki's code is such a relief. My eyes were bleeding from the other code.

### #9Zichu  Members

109
Like
0Likes
Like

Posted 12 July 2012 - 08:43 AM

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.

### #10saejox  Members

714
Like
0Likes
Like

Posted 12 July 2012 - 11:52 AM

emitters vector holds pointers. simple erase or clear would not destroy particles.

you should call delete on them.

### #11stitchs  Members

1361
Like
1Likes
Like

Posted 12 July 2012 - 02:20 PM

Please correct me if I am wrong, but it is to my understanding that "Vector erase iterator out of range" would mean you are passing in a Vector position that is outside that actual size of the vector, meaning that said Vector position doesn't exist?

Also, if you remove "+ emitterNum" from you first removeEmitter function, then you will only be erasing the element at the very beginning of the Vector each time the method is called, which is probably what you don't want/might lead to some particles 'dying' before their life comes to their actual end.

Again, I apologise if any of the information I am providing is incorrect.

Edited by stitchs, 12 July 2012 - 02:20 PM.

### #12Jansic  Members

251
Like
0Likes
Like

Posted 16 July 2012 - 06:30 AM

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.

I suspect the problem is in the calling function - it's probably looping over the length of the array and calling removeEmitter for each index - which won't work correctly because erasing an element from the vector will make it shorter, shifting the other elements up by one..

Jans.

Edited by Jansic, 16 July 2012 - 06:38 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.