• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Fs02

Why destructor is not called ?

14 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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
2

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

2

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.

1

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.

0

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

Share this post


Link to post
Share on other sites

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

1

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

0

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

0

Share this post


Link to post
Share on other sites

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?

2

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0