• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Motoky12

Memory Leak?

11 posts in this topic

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:

[quote]
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:

[CODE]
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;
}
}
}
[/CODE]

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

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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:

[CODE]
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;
}
}
}
[/CODE]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
That can be done more easily

[CODE]
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);
}
[/CODE]
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
emitters vector holds pointers. simple erase or clear would not destroy particles.

you should call delete on them.
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[quote name='Zichu' timestamp='1342104198' post='4958417']
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.
[/quote]
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0