Having problems with destructor of a class object thats in another class object

Started by
15 comments, last by Evil Booger 16 years, 4 months ago
Okay, the problem is that the destructors of my “EnemyShip” objects are not being called. And I am having trouble with this because my “Level” class contains an array of “EnemyShips”. The Level’s destructor runs. I need to know how I can’t force the Level’s destructor to activate the destructors of the EnemyShips. And also, EnemyShip is inherited from “Ship”, that is why in my source code, I give you the destructor of my Ship class. Here is some code. Post if you need more.

Ship::~Ship()
{
	if(surface)
	{
		SDL_FreeSurface(surface);
	}
	if(altsurface)
	{
		SDL_FreeSurface(altsurface);
	}
	if(explosionsurface)
	{
		SDL_FreeSurface(explosionsurface);
	}
	std::cout << "Destructing ship..." << std::endl;
}

class Level
{
public:
	SDL_Surface *background1;
	EnemyShip EnemyShipArray[ENEMYSHIPS_IN_LEVEL];
	//int formation_number;

	Level();
	~Level();
	void ExecuteLevel();
	int FormationDestroyed(int formnum);
	void ActivateFormation(int formnum);
};

Level::~Level()
{
	SDL_FreeSurface(background1);
	std::cout<<"Destructing Level"<<std::endl;
}


Advertisement
Is the destructor for the Ship class virtual?

Anyway, if that isn't the problem, you may need to post more code (e.g. the headers for the Ship, EnemyShip, and Level classes).
No, its not virtual. Is it supposed to be?

Ok I added some more level code.
Quote:Original post by Evil Booger
No, its not virtual. Is it supposed to be?
Yeah, it should be virtual (otherwise, the derived class destructors won't be called when the classes are used polymorphically).

However, it looks like you're storing the EnemyShip's by value, so AFAIK the EnemyShip destructor should be invoked no matter what.

I think we may need to see more code...
Well the EnemyShip class does not have a destructor, it uses the one from the Ship class.

My first two EnemyShip objects destruct perfectly because they are not associated with the Level object. The fifteen other enemyships are copies of the original two that I create using this function:
void CopyEnemyShips(int num_of_ships,					EnemyShip *e_array,					int start_identifier,					EnemyShip *base_ship){	e_array = e_array+start_identifier;	for(int n=0;n<num_of_ships;n++)	{		(e_array + n)->surface = base_ship->surface;		assert((e_array + n)->surface);		(e_array + n)->altsurface = base_ship->altsurface;		(e_array + n)->explosionsurface = base_ship->explosionsurface;		(e_array + n)->laser1.surface = base_ship->laser1.surface;		(e_array + n)->laser2.surface = base_ship->laser2.surface;		(e_array + n)->laser3.surface = base_ship->laser3.surface;		(e_array + n)->laser4.surface = base_ship->laser4.surface;		(e_array + n)->PosRect.x = 64 * n;		(e_array + n)->PosRect.y = 10;		(e_array + n)->shiptype = base_ship->shiptype;		(e_array + n)->SetHealth(base_ship->health);	}}


And this function is called in my "main()" function (after the surface files are loaded) like this:
		CopyEnemyShips(5,level1.EnemyShipArray,0,&Zapper);		CopyEnemyShips(5,level1.EnemyShipArray,5,&Zapper);		CopyEnemyShips(5,level1.EnemyShipArray,10,&Grunt);
But what I'm asking is: Is there a way to force a destructor call?
To force destruction you call 'delete object;' delete will call all the destructors that should be called.

You still did not provide enough code. I would say that the important piece of code is the .h file.

from what you are saying , it seems you are just missing a virtual destructor in your Ship class.

virtual is the implementation of polymorphism in C++. virtual tells the compiler that a call to this function is dynamic. while not using virtual , the calls to the function are static.

so if you only have a class Ship with a function foo(). you dont need to use virtual on this function. the calls to this function will always be static. the same function always gets called.

when you inherit from Ship class with EnemyShip and EnemyShip implement foo(). the call need to become virtual. because now the calls are dynamic. a call to foo() may result to a call for Ship.foo() or EnemyShip.foo(). thats the job of the VTable.

Hope thats help,
Nuno1
Ok Nuno, I think I understand what you're saying. But I didn't think that virtual was necessary because my EnemyShip class does not have a destructor, so the only one to call is the Ship's destructor. And this works fine for the EnemyShips that are not in my Level class.

And I don't think there's any more code I can show except for code from my ship class but that's nothing except a nonvirtual destructor declaration.
Are you calling the level object destructors ? just want to make sure :).
Does EnemyShip actually inherit from Ship?

This topic is closed to new replies.

Advertisement