Weird problem with delete

Started by
13 comments, last by snk_kid 18 years, 10 months ago
Hello, I have a class with the following constructor:

CShipDrone::CShipDrone (void)
{
	CWeapon **weapons = new CWeapon*[1];
	weapons[0] = new CWpnLaser();
	delete weapons[0];
	delete weapons;
}
Doesn't do much, it's just for debugging the error I'm getting. When I create an instance of this class (CShipDrone) and then delete it, like so: CShip *ship = new CShipDrone(); delete ship; the game crashes on the 'delete ship' statement. The class CShipDrone is based on the CShip class, and the CWpnLaser class is based on the CWeapon class. It is interesting that this code works fine when I allocate an instance of the base class, CWeapon, in the constructor rather than the derived class CWpnLaser. Also, there is no crashing if I comment out either 'delete weapons[0]' or 'delete weapons' in the constructor. Why would this be crashing only when I use CWpnLaser? And why does it crash only when I delete the instance of the class that made the allocation? Thank You! Jon Woyame
Advertisement
try this


CShip *ship = NULL;
ship = new CShipDrone();
delete ship;

if that doesnt crash then its not being properly allocated

Sorry I read through quite fast, but from the code snippet it seems that youre allocating with new[] and deleting with the standard delete.

try replacing delete weapons; by delete[] weapons;

Hope this helps

Eric
Still crashes.

I also forgot to mention that it doesn't have anything to do with the CShipDrone class, because it has the same problem when I put the code in another class's constructor and create an instance of it.

Jon
the last line of your constructor should be

delete [] weapons;

since you are technically allocating an array (of one)
also you have not posted your destructor code.

If you are inheriting from CShip and CWeapon make sure your destructors there are virtual.
Quote:Original post by xEricx
try replacing delete weapons; by delete[] weapons;


I didn't notice that, but your right. However the game still crashes when I change it to delete[].

None of the classes involved have destructors.
There are two potential problems here. One is a memory management issue, the other is a faulty destructor.

If it's CWpnLaser's destructor, then the following code should also crash:

CShipDrone::CShipDrone(void){  CWpnLaser foo;}


If it's an allocation issue, start small and work your way up:

CShipDrone::CShipDrone(void){  CWeapon* weapon = new CWpnLaser();  delete weapon;}


If the above code fails, but the first set works... well... sounds like an "operator new" thing. I haven't worked with custom memory managers much, so I really can't help you there.


How about this:
CShipDrone::CShipDrone(void){  std::vector<CWeapon*> weapons;  weapons.push_back( new CWpnLaser() );  delete weapons[0];}


Don't mess with dynamic memory allocation when you don't have to. STL containers and boost::smart_ptr will virtually remove your need to ever call "delete".

http://www.boost.org/libs/smart_ptr/smart_ptr.htm
--Mark
First, it would be easier to change this to a :

std::vector<CWeapon* > m_pWeapons;

then, m_pWeapons.push_back(new CWpnLaser());

when destroying your ship, iterate through every items in m_pWeapons and delete them;

Also, when does it crash? In CShipDrone's ctor? in CShipDrone's dtor? or in another moment?

Can you also give us the error message you get when it crashes? Did you try to use a debugger?

Thanks
Is CWpnLaser class an inherited class from CWeapon? Because... weapons[0] is a pointer to a CWeapon, yet in your new you say CWpnLaser.

I usually try to avoid double pointers when I can, there is usually a better way of doing things. Also I would add to your code a NULL check.
This code made it crash:

CShipDrone::CShipDrone(void)
{
CWeapon* weapon = new CWpnLaser();
delete weapon;
}

However it didn't crash with this:

CShipDrone::CShipDrone(void)
{
CWpnLaser foo;
}

It doesn't crash with this either:

CShipDrone::CShipDrone(void)
{
CWeapon* weapon = new CWpnLaser();
}

This is a very strange problem. I've never seen anything like this.

Jon

This topic is closed to new replies.

Advertisement