• 14
• 12
• 9
• 10
• 9

Weird problem with delete

This topic is 4678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
try this

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

if that doesnt crash then its not being properly allocated

Share on other sites
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

Share on other sites
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

Share on other sites
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.

Share on other sites
Quote:
 Original post by xEricxtry 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.

Share on other sites
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.

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

Share on other sites
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

Share on other sites
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.

Share on other sites

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