Problem clearing btAlignedObjectArray<btCollisionShape*>

beatriz
Posts: 4
Joined: Fri Oct 11, 2013 9:15 am

Problem clearing btAlignedObjectArray<btCollisionShape*>

Post by beatriz »

Hi!
I'm trying to delete a body of the world, and then creating it again with a new position. All works fine but I have some memory leacks then. I've discovered that they are because I am not clearing correctly the btAlignedObjectArray<btCollisionShape*> m_collisionShapes.

What I want to do is clear only the reference to the body I want to delete and the add that reference again in the same position as it was before, so then the rest of the bodies of my world are not modified... is it that possible?
how can I do it?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Problem clearing btAlignedObjectArray<btCollisionShape*>

Post by Flix »

Do not delete the body collision shape when you delete the body, and reuse it when you recreate it.

AFAIR the "btAlignedObjectArray<btCollisionShape*> m_collisionShapes" is used in the demos to collect the collision shapes that will be deleted when the dynamic world is destroyed. This is because multiple bodies can share the same collision shape (that's why you should not delete the collision shape when you delete the body).

Memory leaks can happen when the collision shapes inside m_collisionShapes are destroyed if the same collision shape is added to it more than once.

Other memory leaks can happen if you do not delete by yourself collision shapes that have not been added to m_collisionshapes.
beatriz wrote:What I want to do is clear only the reference to the body I want to delete and the add that reference again in the same position as it was before, so then the rest of the bodies of my world are not modified... is it that possible?
how can I do it?
I don't understand what you want to achieve... a btCollisionObject/btRigidBody is different from a btCollisionShape. Do you have problems with btCollisionObjects/btRigidBodies or with btCollisionShapes?
beatriz
Posts: 4
Joined: Fri Oct 11, 2013 9:15 am

Re: Problem clearing btAlignedObjectArray<btCollisionShape*>

Post by beatriz »

What I want to achieve is to delete a body and then create it again. All the bodies on my world have the same collision shape.
I have checked and my memory leaks appear when I do this:

Code: Select all

		const btVector3 AngularFactor = body->getAngularFactor();								
		const btScalar Restitution = body->getRestitution();
		btQuaternion Orientation = body->getOrientation();


		//remove the rigid body of the world

          m_dynamicsWorld->removeRigidBody(m_bodyList.GetAt(sel));
	  	 m_dynamicsWorld->removeCollisionObject(m_bodyList.GetAt(sel));
         delete m_bodyList.GetAt(sel)->getMotionState();

         delete m_bodyList.GetAt(sel);


		// create again the body with the new position but the same properties as before	


			btCollisionShape* dinamicShape = new btBoxShape(btVector3(0.5,0.5,0.5));

			m_trans.setIdentity();
			btVector3 pos(xPos, yPos, zPos);
			m_trans.setOrigin(pos);
			m_trans.setRotation(Orientation);

			int mass = 3.f;

			btVector3 localInertia(0,0,0);
			
			dinamicShape->calculateLocalInertia(mass,localInertia);

			//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
			btDefaultMotionState* myMotionState = new btDefaultMotionState(m_trans);
			btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,dinamicShape,localInertia);
			m_body = new btRigidBody(rbInfo);
and then I add the body to the world again.
Do am I doing something wrong?
beatriz
Posts: 4
Joined: Fri Oct 11, 2013 9:15 am

Re: Problem clearing btAlignedObjectArray<btCollisionShape*>

Post by beatriz »

I've already fix it! :D
I didn't need to clear the m_collisionShapes as you told me, the only thing I need to do is to push_back the new collision shape I've created!

Thanks Flix :wink: