Why destructor is not called ?

Started by
13 comments, last by Fs02 10 years, 11 months ago

Hi, i'm making a simple top down shooter game which have bullets entity, my problem is the bullet entity dtor is not called when i call the delete operator.

here's some of my code :

CBullet.hpp


namespace CEntity
{
	class CBullet
	{
	private:
		b2Body* m_Body;
	public:
		CBullet(b2World* world, const b2Vec2& normal, const b2Vec2& pos, float rot);
		~CBullet();
	};
}

CBullet.cpp


CEntity::CBullet::CBullet(b2World* world, const b2Vec2& normal, const b2Vec2& pos, float rot)
{
//
    fixDef.filter.categoryBits = CEntity::BULLET;
    m_Body->CreateFixture(&fixDef);
//
    m_Body->SetUserData(this);
    
}

CEntity::CBullet::~CBullet()
{
	m_Body->GetWorld()->DestroyBody(m_Body);
}

Cgun (responsible for creating the bullet with new operator)


void CEntity::CGun::shoot(const b2Vec2& normal, const b2Vec2& pos, float rot)
{
//
   new CEntity::CBullet(m_World, normal, pos, rot);
//
}

ContactListener (where the deletion should occur)


	void EndContact(b2Contact* contact)
	{
		b2Fixture* fixA		= contact->GetFixtureA();
		b2Fixture* fixB		= contact->GetFixtureB();
		
		if (fixA->GetFilterData().categoryBits	== CEntity::BULLET)
		{
			delete static_cast<CEntity::CBullet*>(fixA->GetUserData());
		}

		if (fixB->GetFilterData().categoryBits	== CEntity::BULLET)
		{
			delete static_cast<CEntity::CBullet*>(fixB->GetUserData());;
		}
	}

that's all, im using box2d by the way, any help would be apreciated :)

Surya

PS: Sorry for my bad english

Advertisement

Is the class CEntity::CBullet known at the point of deletion?

That is, is CBullet.hpp included in the source code file that the function EndContact is in?

If not, and CEntity::CBullet is just forward declared (with 'class CBullet;' somewhere) then that could explain the behaviour you're seeing. Although I believe most compilers would warn you.

edit: There's a discussion about this behaviour here: http://stackoverflow.com/questions/4325154/delete-objects-of-incomplete-type

Cgun (responsible for creating the bullet with new operator)


void CEntity::CGun::shoot(const b2Vec2& normal, const b2Vec2& pos, float rot)
{
//
   new CEntity::CBullet(m_World, normal, pos, rot);
//
}

Do you actually store the return value from new anywhere? Because otherwise you leak it and won't be able to delete it => no destructor will be called.

EDIT: Hard to edit posts with quote boxes in them, isn't it...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Is the class CEntity::CBullet known at the point of deletion?

yes it is, i've try calling one dummy member function which write some output in the console and it's works fine.


//something like this
static_cast<CEntity::CBullet*>(fixA->GetUserData())->writeToConsole();

That is, is CBullet.hpp included in the source code file that the function EndContact is in?

it's included

Do you actually store the return value from new anywhere? Because otherwise you leak it and won't be able to delete it => no destructor will be called.

i'm not storing the new value everywhere except on m_Body->setUserData(this); in CBullet constructor, thats the only way to call CBullet after the new operator.

void CEntity::CGun::shoot(const b2Vec2& normal, const b2Vec2& pos, float rot)
{
//
new CEntity::CBullet(m_World, normal, pos, rot);
//
}

This is a problem. You aren't saving this in a class internal variable, you aren't returning the new CBullet and you aren't saving into a variable passed by reference. The system is grabbing a heap of memory and tossing it into a giant pile of garbage. I also suspect this to be your problem.

This is a problem. You aren't saving this in a class internal variable, you aren't returning the new CBullet and you aren't saving into a variable passed by reference. The system is grabbing a heap of memory and tossing it into a giant pile of garbage. I also suspect this to be your problem.

If you look again, in the Bullet constructor the this pointer is stored into a user data slot on the rigid body. In contact resolution, it is taken back out again, cast to Bullet and deleted there.

The CBullet constructor does store this though (in m_body), so it could feasibly be used to later delete it. Not a design I would use...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This is a problem. You aren't saving this in a class internal variable, you aren't returning the new CBullet and you aren't saving into a variable passed by reference. The system is grabbing a heap of memory and tossing it into a giant pile of garbage. I also suspect this to be your problem.

If you look again, in the Bullet constructor the this pointer is stored into a user data slot on the rigid body. In contact resolution, it is taken back out again, cast to Bullet and deleted there.

Oh, wow OK.

The CBullet constructor does store this though (in m_body), so it could feasibly be used to later delete it. Not a design I would use...

Nor would I, for the simple fact that looking at that line does not make it readily apparent what is happening. In fact, it makes it look like a bug to anyone who doesn't have insider knowledge (which I just proved lol).

And how exactly do you know the destructor isn't being called?

Anyway, unless I don't understand Box2D, you're likely invoking undefined behavior or creating problems. From the Box2D manual (section 9.4, post-solve):

It is tempting to implement game logic that alters the physics world inside a contact callback. For example, you may have a collision that applies damage and try to destroy the associated actor and its rigid body. However, Box2D does not allow you to alter the physics world inside a callback because you might destroy objects that Box2D is currently processing, leading to orphaned pointers.
The recommended practice for processing contact points is to buffer all contact data that you care about and process it after the time step. You should always process the contact points immediately after the time step; otherwise some other client code might alter the physics world, invalidating the contact buffer. When you process the contact buffer you can alter the physics world, but you still need to be careful that you don't orphan pointers stored in the contact point buffer. The testbed has example contact point processing that is safe from orphaned pointers.
If I'm reading the manual right, your destructor is actually being called and you are indeed deleting your object, however, Box2D is preventing you from destroying the body (which is why I'm guessing you think the destructor isn't being called, because it doesn't look like it is because the body still exists in the physics world).
In short, you should would:
  • make 100% sure your destructor isn't actually being called, because I'm betting it actually is being called
  • not have that confusing constructor (so it doesn't look like you're leaking memory)
  • not modify the physics world in a contact callback
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

And how exactly do you know the destructor isn't being called?

I wrote a line in destructor definition that should write something into console window but there is no output shown.

Anyway, unless I don't understand Box2D, you're likely invoking undefined behavior or creating problems. From the Box2D manual (section 9.4, post-solve):
Quote

It is tempting to implement game logic that alters the physics world inside a contact callback. For example, you may have a collision that applies damage and try to destroy the associated actor and its rigid body. However, Box2D does not allow you to alter the physics world inside a callback because you might destroy objects that Box2D is currently processing, leading to orphaned pointers.

The recommended practice for processing contact points is to buffer all contact data that you care about and process it after the time step. You should always process the contact points immediately after the time step; otherwise some other client code might alter the physics world, invalidating the contact buffer. When you process the contact buffer you can alter the physics world, but you still need to be careful that you don't orphan pointers stored in the contact point buffer. The testbed has example contact point processing that is safe from orphaned pointers.

i have try that one to but seems that wasn't the problem, the destructor still not being called

If I'm reading the manual right, your destructor is actually being called and you are indeed deleting your object, however, Box2D is preventing you from destroying the body (which is why I'm guessing you think the destructor isn't being called, because it doesn't look like it is because the body still exists in the physics world)

yes, you're right. in this case, my game should run into runtime error if the deletion really occur

This topic is closed to new replies.

Advertisement