This is a problem connected to C++, I think, but I can't find a proper solution for it. Best I've found this far is from here:
http://www.cprogramming.com/tutorial/dynamic_memory_allocation.htmlbut it doesn't work for me.
I have a class named panzergrenade. Whenever a tank in the game shoot a grenade, I create a new grenade, put it in an array and have it live in the game until it hits something
When it hit, I make the grenade invisible and create another class instance which is an explosion, added to another array, at the very same place.
When the explosion start, I want to delete the panzergrenade to make room in the array for new ones and to retain memory, same for the explosion when it has finished exploding, but I get a debug error doing that.
class Panzergrenade
{
public :
Panzergrenade(double out_velocity, double out_angel, bool out_is_drawable); //construction
~Panzergrenade(){};
double target_x;
double target_y;
double velocity;
double angel;
bool is_drawable;
sf::Sprite Sprite; // sprite for the grenade
sf::Image Image; //imagefile, just an image and no spritesheet
};
Panzergrenade::Panzergrenade(double out_velocity, double out_angel, bool out_is_drawable);
{
if (!Image.LoadFromFile("grenade.png")) //load picture
{
std::cout << "Can't find picture: grenade.png " << std::endl;
}
velocity=out_velocity;
angel=out_angel;
is_drawable = out_is_drawable;
Sprite.SetRotation(angel);//Give the same angel as the tank when creatde
Sprite.SetImage(Image); //Give correct picture to the class
}
//Array to save the grenades in, no problem there
Panzergrenade *grenadelist[100];//max 100 grenades in game
int grenade_i=0; //0-100 shows where we are in the list
int si =0; //counter 0-100 when checking items in list
And then, at creation:
grenadelist[grenade_i] = new Panzergrenade(1.0f,tank1.sprite.GetRotation(), true);
grenade_i++;
//velocity / what tank the rotation is copied from / if it should be drawn
What I do right now, when a grenade hit, is that I just set the velocity to 0 and put is_drawable=false.
The grenade exists but it isn't shown anymore. An explosion is created on top of the invisible grenade and is also set to invisible after the explosion is finished.
This works fine for 100 grenades, the problem start to come if I want to have more than 100 grenades.
If I reset grenade_i to 0 and try to add a new grenade on the occupide place, no grenade is created at all. No error messages, nothing.
I assume this could create a horrible memory leak if it actually worked though, since the first created grenade would loose it's pointer adress and become impossible to find for deletion.
If I instead of making the grenade invisible tries to delete it:
delete [] grenadelist[si];
The program compiles but when the actual deletion is about take place, the program crasches with:
Debug Assertion Failed!
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
(I've also tried:
delete [si] grenadelist;
to no avail, it crasches too).
If I try to delete the whole list when closing down the program with:
delete [] grenadelist;
I get another error:
Debug Assertion Failed!
Expression:_CrtIsValidHeapPointer(pUserData)
So the questions are:
*How am I supposed to delete an item in an array to free the memory and make it possible to save a new class instance in it's place?
*How do I delete a full list of class instances put in an array when the program ends to release memory in a nice way?
*If I fill an array, how do I do if I want to reuse a place in the array for a new class instance? (starting from 0 again)