Jump to content
  • Advertisement
Sign in to follow this  
Fs02

Why destructor is not called ?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Edited by C0lumbo

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!