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?
Problem clearing btAlignedObjectArray<btCollisionShape*>
-
- Posts: 4
- Joined: Fri Oct 11, 2013 9:15 am
-
- Posts: 456
- Joined: Tue Dec 25, 2007 1:06 pm
Re: Problem clearing btAlignedObjectArray<btCollisionShape*>
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.
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.
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 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?
-
- Posts: 4
- Joined: Fri Oct 11, 2013 9:15 am
Re: Problem clearing btAlignedObjectArray<btCollisionShape*>
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:
and then I add the body to the world again.
Do am I doing something wrong?
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);
Do am I doing something wrong?
-
- Posts: 4
- Joined: Fri Oct 11, 2013 9:15 am
Re: Problem clearing btAlignedObjectArray<btCollisionShape*>
I've already fix it!
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

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
