Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Why destructor is not called ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 Fs02   Members   -  Reputation: 416

Like
0Likes
Like

Posted 27 April 2013 - 08:02 AM

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


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2398

Like
2Likes
Like

Posted 27 April 2013 - 08:09 AM

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, 27 April 2013 - 08:11 AM.


#3 Paradigm Shifter   Crossbones+   -  Reputation: 5410

Like
2Likes
Like

Posted 27 April 2013 - 08:13 AM

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

#4 Fs02   Members   -  Reputation: 416

Like
1Likes
Like

Posted 27 April 2013 - 09:57 AM

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.


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


#5 Azaral   Members   -  Reputation: 463

Like
0Likes
Like

Posted 27 April 2013 - 10:31 AM

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.



#6 FLeBlanc   Crossbones+   -  Reputation: 3109

Like
1Likes
Like

Posted 27 April 2013 - 10:38 AM

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.

#7 Paradigm Shifter   Crossbones+   -  Reputation: 5410

Like
1Likes
Like

Posted 27 April 2013 - 10:39 AM

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

#8 Azaral   Members   -  Reputation: 463

Like
0Likes
Like

Posted 27 April 2013 - 10:44 AM

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



#9 Cornstalks   Crossbones+   -  Reputation: 6991

Like
3Likes
Like

Posted 27 April 2013 - 12:57 PM

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


[ 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 ]

#10 Fs02   Members   -  Reputation: 416

Like
0Likes
Like

Posted 27 April 2013 - 09:32 PM

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


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


#11 MrP   Members   -  Reputation: 565

Like
2Likes
Like

Posted 28 April 2013 - 02:20 AM

You're trying to retrieve the CBullet object pointer from the fixture's userdata but you're storing it as the body's userdata - I think this may be the problem?



#12 kunos   Crossbones+   -  Reputation: 2207

Like
1Likes
Like

Posted 28 April 2013 - 03:15 AM

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

 

that's really beyond hacky :P .. but I expect nothing less from a juventus fan :D

 

I would say Columbo's is the right diagnosis... I bet there is a compiler warning saying something in the lines of "delete called on undefined object, no destructor called".


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#13 Fs02   Members   -  Reputation: 416

Like
0Likes
Like

Posted 28 April 2013 - 04:53 AM

You're trying to retrieve the CBullet object pointer from the fixture's userdata but you're storing it as the body's userdata - I think this may be the problem?

 

yeah, you're right, thanks very much and now my program throw a runtime error just like what i hope biggrin.png

i little tweak and it should be no problem anymore cool.png

 

but i still don't understand why the other function(except destruction) still can be called even if i'm retrieve the wrong userdata unsure.png


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


#14 Paradigm Shifter   Crossbones+   -  Reputation: 5410

Like
1Likes
Like

Posted 28 April 2013 - 05:04 AM

Are they non-virtual? The correct function will be called then (the this pointer would be junk though if you call it on the wrong object type). Virtual functions (I'm guessing the destructor is virtual due to overriding a virtual destructor in a base class) need a valid this pointer.

 

Presumably the userdata you tried to delete was null before the fix... which would explain why no destructor was called (deleting a null pointer is valid, no destructor is called).

 

Since you were logging stuff when you called the destructor you should consider logging allocations (log the type and address in the constructor) and deallocations (same deal in the destructor) to help debugging similar problems later on.


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

#15 Fs02   Members   -  Reputation: 416

Like
0Likes
Like

Posted 28 April 2013 - 08:02 PM

Since you were logging stuff when you called the destructor you should consider logging allocations (log the type and address in the constructor) and deallocations (same deal in the destructor) to help debugging similar problems later on.

 

Thanks for the explanation although i don't know how to do the allocations and deallocations logging things because usually i'm using callstack to track null pointer :D


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS