[solved] list<T> stores instances, deleting them impos

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Flexxx
Posts: 4
Joined: Tue Oct 28, 2008 7:12 pm

[solved] list<T> stores instances, deleting them impos

Post by Flexxx »

Hello!

I've startet with an Irrlicht based game some days ago and I'm new to Irrlicht usage and I'm not a very good C++ developer.

So I use irr::core::list<T> to store the player's weapons (following method is a quick temporary one to achieve early results):

Code: Select all

void Player::addWeapon(f32 damage)
{
    Weapon* weapon = new Weapon(damage);
    mWeapons.push_back( weapon );

    if(mWeapons.getSize() == 1)
        mCurrentWeapon = mWeapons.begin();
}
I delete the Weapon instances in the destructor of the class Player(the destructor of Player ist called; I tested it):

Code: Select all

Player::~Player()
{
    list<Weapon*>::Iterator i;
    for(i = mWeapons.begin(); i != mWeapons.end(); i++)
    {
      delete (*i);
    }
}
Unfortunately Weapon's destructor ist never called. Apart from that, I think, not allocating the on the heap my calling new would be a very little bit faster. But that doesn't matter to my problem.

Additionaly this is an excerpt of declaration of the class Player:

Code: Select all

        list<Weapon*> mWeapons;

        list<Weapon*>::Iterator mCurrentWeapon;
I already use the search function to find answers. This was unsuccessful.

Does anyone knows a solution to my problem? I would be happy if someone could help. ;-)

btw, sorry for eventually incorrectniss of words and grammar, im not from an english speaking country
Last edited by Flexxx on Sat Nov 01, 2008 9:12 pm, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You are deleting the pointer, not the object. This is because you are dereferencing just the Iterator, which returns the element (a pointer).
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

hybrid wrote:You are deleting the pointer, not the object. This is because you are dereferencing just the Iterator, which returns the element (a pointer).
Yes, but deleting the pointer (the contents of the iterator) should result in the destructor of the pointee being called...

Code: Select all

Weapon* w = new Weapon;
delete w; // this calls Weapon::~Weapon()
His code should be no different than writing...

Code: Select all

    list<Weapon*>::Iterator i; 
    for(i = mWeapons.begin(); i != mWeapons.end(); i++) 
    { 
      Weapon* w = (*i);
      delete w; 
    } 
That is legitimate code. It isn't absolutely clear if the destructor for Player is getting called. If it is, then the second thing to to check for would be an empty list. If the loop is running, but the destructor still isn't getting called, then I'm starting to wonder if Weapon is a base class and you are actually instantiating derived classes and putting them in the list in addWeapon(). For that you would want a virtual destructor in the base class.

The only other thing that I could think of is that you may be seeing some sort of compiler bug. I seriously doubt it, but I've seen things like this before. You could try removing the parens around *i in the delete call.

Travis
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Ok, I have no idea why it is not getting deleted, the list is not using a custom allocator or anything. But you should really just add the weapons directly to the list (Not as a pointer), and delete them using "erase()", it's more efficient too, list elements are already on the heap, no need for another indirection.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

That won't work very well if this is prototype code and Weapon is actually a base class and there will be derived weapons like Sword, Mace, Katana and Nunchuck. In that case he most definitely would want a pointer.

Of course I could be wrong and this could just be one of those cases where the OP isn't really a big C++ guy.

In any case, has someone even attempted to reproduce the problem? Out of curiousity, which compiler (version) and what version of Irrlicht are being used here?

Travis
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Irrlicht SVN trunk, MSVC++ 9.0

Code: Select all

#include "irrlicht.h"

using namespace irr;
using namespace core;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

class Weapon
{
public:
	~Weapon()
	{
		(void)printf("~Weapon()\n");
	}
};

class Player
{
public:
	list<Weapon*> mWeapons;

	void addWeapon()
	{
	    Weapon* weapon = new Weapon();
	    mWeapons.push_back( weapon );
	}

	~Player()
	{
		(void)printf("~Player()\n");
		list<Weapon*>::Iterator i;
		for(i = mWeapons.begin(); i != mWeapons.end(); i++)
		{
			delete (*i);
		}
	} 
};


int main()
{
	Player * player = new Player();
	player->addWeapon();
	player->addWeapon();
	player->addWeapon();
	delete player;
}
Result is as expected:

Code: Select all

~Player()
~Weapon()
~Weapon()
~Weapon()
EBKAC?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Flexxx
Posts: 4
Joined: Tue Oct 28, 2008 7:12 pm

Post by Flexxx »

Now I know that mWeapons.push_back( weapon ); gets a valid pointer, but the size of mWeapons at the beginning in ~Player is 0. So the for-loop doesn't execute.

Code: Select all

    printf("%u\n",mWeapons.getSize());
    mWeapons.push_back( weapon );
    printf("%u\n",mWeapons.getSize());
This prints:

Code: Select all

3998176
3998177
I use Irrlicht 1.4 with MingW on Win XP SP2:

Code: Select all

C:\MinGW\bin>mingw32-c++.exe --version
mingw32-c++.exe (GCC) 3.4.5 (mingw special)
I use the precompiled binaries of Irrlicht.
Flexxx
Posts: 4
Joined: Tue Oct 28, 2008 7:12 pm

Post by Flexxx »

lol, an uninitialised variable in an other class seemed to cause the problem. Sorry for that time stealing.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

No problem, it's good for us to validate the basic functionality of Irrlicht from time to time. I'm glad you solved it.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply