diff --git a/Demos/AllBulletDemos/DemoEntries.cpp b/Demos/AllBulletDemos/DemoEntries.cpp index ae9d9363e..6592ddc40 100644 --- a/Demos/AllBulletDemos/DemoEntries.cpp +++ b/Demos/AllBulletDemos/DemoEntries.cpp @@ -18,31 +18,93 @@ subject to the following restrictions: #include "../CcdPhysicsDemo/CcdPhysicsDemo.h" #include "../BspDemo/BspDemo.h" #include "../BasicDemo/BasicDemo.h" -#include "../ConcaveDemo/ConcaveDemo.h" -#include "../ConvexDecompositionDemo/ConvexDecompositionDemo.h" -#include "../RagdollDemo/RagdollDemo.h" -#include "../GimpactTestDemo/GimpactTestDemo.h" -#include "../Raytracer/Raytracer.h" -#include "../GjkConvexCastDemo/LinearConvexCastDemo.h" -#include "../VehicleDemo/VehicleDemo.h" -#include "../ConstraintDemo/ConstraintDemo.h" - +#include "../ConcaveDemo/ConcaveDemo.h" +#include "../ConvexDecompositionDemo/ConvexDecompositionDemo.h" +#include "../RagdollDemo/RagdollDemo.h" +#include "../GimpactTestDemo/GimpactTestDemo.h" +#include "../Raytracer/Raytracer.h" +#include "../GjkConvexCastDemo/LinearConvexCastDemo.h" +#include "../VehicleDemo/VehicleDemo.h" +#include "../ConstraintDemo/ConstraintDemo.h" + +#include "GlutStuff.h"//OpenGL stuff +#include "BMF_Api.h"//font stuff + +extern int gNumAlignedAllocs; +extern int gNumAlignedFree; +extern int gTotalBytesAlignedAllocs; + +class btEmptyDebugDemo : public DemoApplication +{ +public: + btEmptyDebugDemo() + { + + } + + virtual void clientMoveAndDisplay() + { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + float xOffset = 10.f; + float yStart = 20.f; + float yIncr = 20.f; + char buf[124]; + + + glColor3f(0, 0, 0); + + setOrthographicProjection(); + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gNumAlignedAllocs= %d",gNumAlignedAllocs); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gNumAlignedFree= %d",gNumAlignedFree); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"# alloc-free = %d",gNumAlignedAllocs-gNumAlignedFree); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; +#ifdef BT_DEBUG_MEMORY_ALLOCATIONS + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gTotalBytesAlignedAllocs = %d",gTotalBytesAlignedAllocs); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; +#endif //BT_DEBUG_MEMORY_ALLOCATIONS + + glFlush(); + glutSwapBuffers(); + + } + + static DemoApplication* Create() + { + btEmptyDebugDemo* demo = new btEmptyDebugDemo(); + demo->myinit(); + return demo; + } -btDemoEntry g_demoEntries[] = -{ - {"RagdollDemo",RagdollDemo::Create}, - {"ConvexDecomposition",ConvexDecompositionDemo::Create}, - {"CcdPhysicsDemo", CcdPhysicsDemo::Create}, - {"BasicDemo", BasicDemo::Create}, - {"BspDemo", BspDemo::Create}, - {"ConcaveDemo",ConcaveDemo::Create}, - {"Gimpact Test", GimpactConcaveDemo::Create}, - {"Raytracer Test",Raytracer::Create}, - {"GjkConvexCast",LinearConvexCastDemo::Create}, - {"VehicleDemo",VehicleDemo::Create}, - {"ConstraintDemo",ConstraintDemo::Create}, - {0, 0} - - +}; + +btDemoEntry g_demoEntries[] = +{ + {"BasicDemo", BasicDemo::Create}, + {"RagdollDemo",RagdollDemo::Create}, + {"ConvexDecomposition",ConvexDecompositionDemo::Create}, + {"CcdPhysicsDemo", CcdPhysicsDemo::Create}, + {"BspDemo", BspDemo::Create}, + {"ConcaveDemo",ConcaveDemo::Create}, + {"Gimpact Test", GimpactConcaveDemo::Create}, + {"Raytracer Test",Raytracer::Create}, + {"GjkConvexCast",LinearConvexCastDemo::Create}, + {"VehicleDemo",VehicleDemo::Create}, + {"ConstraintDemo",ConstraintDemo::Create}, + {"MemoryLeakChecker",btEmptyDebugDemo::Create}, + {0, 0} }; diff --git a/Demos/BasicDemo/BasicDemo.cpp b/Demos/BasicDemo/BasicDemo.cpp index 3538983b2..ed2054992 100644 --- a/Demos/BasicDemo/BasicDemo.cpp +++ b/Demos/BasicDemo/BasicDemo.cpp @@ -124,7 +124,7 @@ void BasicDemo::initPhysics() setCameraDistance(btScalar(50.)); - btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); + m_collisionConfiguration = new btDefaultCollisionConfiguration(); #ifdef USE_PARALLEL_DISPATCHER @@ -147,7 +147,7 @@ void BasicDemo::initPhysics() m_dispatcher = new SpuGatheringCollisionDispatcher(threadSupport,maxNumOutstandingTasks,collisionConfiguration); #else - m_dispatcher = new btCollisionDispatcher(collisionConfiguration); + m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); #endif //USE_PARALLEL_DISPATCHER #define USE_SWEEP_AND_PRUNE 1 @@ -172,9 +172,9 @@ void BasicDemo::initPhysics() #ifdef USE_SIMPLE_DYNAMICS_WORLD //btSimpleDynamicsWorld doesn't support 'cache friendly' optimization, so disable this sol->setSolverMode(btSequentialImpulseConstraintSolver::SOLVER_RANDMIZE_ORDER); - m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,collisionConfiguration); + m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration); #else - m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,collisionConfiguration); + m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration); #endif //USE_SIMPLE_DYNAMICS_WORLD m_dynamicsWorld->getDispatchInfo().m_enableSPU = true; @@ -246,6 +246,11 @@ void BasicDemo::exitPhysics() for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) { btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; + btRigidBody* body = btRigidBody::upcast(obj); + if (body && body->getMotionState()) + { + delete body->getMotionState(); + } m_dynamicsWorld->removeCollisionObject( obj ); delete obj; } @@ -269,6 +274,8 @@ void BasicDemo::exitPhysics() //delete dispatcher delete m_dispatcher; + delete m_collisionConfiguration; + } diff --git a/Demos/BasicDemo/BasicDemo.h b/Demos/BasicDemo/BasicDemo.h index 89ad89910..9b92ca8fd 100644 --- a/Demos/BasicDemo/BasicDemo.h +++ b/Demos/BasicDemo/BasicDemo.h @@ -24,6 +24,7 @@ class btOverlappingPairCache; class btCollisionDispatcher; class btConstraintSolver; struct btCollisionAlgorithmCreateFunc; +class btDefaultCollisionConfiguration; ///BasicDemo is good starting point for learning the code base and porting. class BasicDemo : public DemoApplication @@ -42,8 +43,17 @@ class BasicDemo : public DemoApplication btCollisionAlgorithmCreateFunc* m_sphereBoxCF; btCollisionAlgorithmCreateFunc* m_boxSphereCF; + btDefaultCollisionConfiguration* m_collisionConfiguration; + public: + BasicDemo() + { + } + virtual ~BasicDemo() + { + exitPhysics(); + } void initPhysics(); void exitPhysics(); diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp index e752380fa..da0a6f050 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp @@ -116,39 +116,7 @@ int shapeIndex[maxNumObjects]; // btPoint3(50,0,0)); static const int numShapes = 4; -btCollisionShape* shapePtr[numShapes] = -{ - ///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate. - ///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346 - -//#define USE_GROUND_PLANE 1 -#ifdef USE_GROUND_PLANE - new btStaticPlaneShape(btVector3(0,1,0),0.5), -#else - new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200)), -#endif - -#ifdef DO_BENCHMARK_PYRAMIDS - new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)), -#else - new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)), -#endif - - //new btSphereShape (CUBE_HALF_EXTENTS), - //new btCapsuleShape(0.5*CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS), - //new btCylinderShape (btVector3(1-gCollisionMargin,CUBE_HALF_EXTENTS-gCollisionMargin,1-gCollisionMargin)), - //new btConeShapeX(CUBE_HALF_EXTENTS,2.f*CUBE_HALF_EXTENTS), - - new btSphereShape (CUBE_HALF_EXTENTS), - - //new btBU_Simplex1to4(btPoint3(-1,-1,-1),btPoint3(1,-1,-1),btPoint3(-1,1,-1),btPoint3(0,0,1)), - - //new btEmptyShape(), - - new btBoxShape (btVector3(0.4,1,0.8)) - -}; - +btCollisionShape* shapePtr[numShapes] = {0,0,0,0}; void CcdPhysicsDemo::createStack( btCollisionShape* boxShape, float halfCubeSize, int size, float zPos ) { @@ -363,6 +331,39 @@ float myFrictionModel( btRigidBody& body1, btRigidBody& body2, btManifoldPoint& void CcdPhysicsDemo::initPhysics() { + shapePtr[0] = + ///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate. + ///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346 + +//#define USE_GROUND_PLANE 1 +#ifdef USE_GROUND_PLANE + new btStaticPlaneShape(btVector3(0,1,0),0.5); +#else + new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200)); +#endif + + shapePtr[1] = +#ifdef DO_BENCHMARK_PYRAMIDS + new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)); +#else + new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)); +#endif + + shapePtr[2] = + //new btSphereShape (CUBE_HALF_EXTENTS); + //new btCapsuleShape(0.5*CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS); + //new btCylinderShape (btVector3(1-gCollisionMargin,CUBE_HALF_EXTENTS-gCollisionMargin,1-gCollisionMargin)); + //new btConeShapeX(CUBE_HALF_EXTENTS,2.f*CUBE_HALF_EXTENTS); + + new btSphereShape (CUBE_HALF_EXTENTS); + + //new btBU_Simplex1to4(btPoint3(-1,-1,-1),btPoint3(1,-1,-1),btPoint3(-1,1,-1),btPoint3(0,0,1)); + + //new btEmptyShape(); + + shapePtr[3] = new btBoxShape (btVector3(0.4,1,0.8)); + + #ifdef DO_BENCHMARK_PYRAMIDS setCameraDistance(2.5f); #endif diff --git a/Demos/OpenGL/DemoApplication.cpp b/Demos/OpenGL/DemoApplication.cpp index 8bcafaef6..4e7f39ae9 100644 --- a/Demos/OpenGL/DemoApplication.cpp +++ b/Demos/OpenGL/DemoApplication.cpp @@ -43,6 +43,7 @@ extern int gNumDeepPenetrationChecks; extern int gNumGjkChecks; extern int gNumAlignedAllocs; extern int gNumAlignedFree; +extern int gTotalBytesAlignedAllocs; #endif // @@ -52,6 +53,7 @@ DemoApplication::DemoApplication() : m_dynamicsWorld(0), m_pickConstraint(0), +m_shootBoxShape(0), m_cameraDistance(15.0), m_debugMode(0), m_ele(20.f), @@ -75,6 +77,8 @@ m_pickConstraint(0), DemoApplication::~DemoApplication() { + if (m_shootBoxShape) + delete m_shootBoxShape; } @@ -461,14 +465,19 @@ void DemoApplication::shootBox(const btVector3& destination) startTransform.setIdentity(); btVector3 camPos = getCameraPosition(); startTransform.setOrigin(camPos); -//#define TEST_UNIFORM_SCALING_SHAPE 1 + + if (!m_shootBoxShape) + { + //#define TEST_UNIFORM_SCALING_SHAPE 1 #ifdef TEST_UNIFORM_SCALING_SHAPE btConvexShape* childShape = new btBoxShape(btVector3(1.f,1.f,1.f)); - btUniformScalingShape* boxShape = new btUniformScalingShape(childShape,0.5f); + m_shootBoxShape = new btUniformScalingShape(childShape,0.5f); #else - btCollisionShape* boxShape = new btBoxShape(btVector3(0.5f,0.5f,0.5f)); + m_shootBoxShape = new btBoxShape(btVector3(0.5f,0.5f,0.5f)); #endif// - btRigidBody* body = this->localCreateRigidBody(mass, startTransform,boxShape); + } + + btRigidBody* body = this->localCreateRigidBody(mass, startTransform,m_shootBoxShape); btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]); linVel.normalize(); @@ -701,7 +710,8 @@ btRigidBody* DemoApplication::localCreateRigidBody(float mass, const btTransform btRigidBody* body = new btRigidBody(mass,myMotionState,shape,localInertia); #else - btRigidBody* body = new btRigidBody(mass,startTransform,shape,localInertia); + btRigidBody* body = new btRigidBody(mass,0,shape,localInertia); + body->setWorldTransform(startTransform); #endif// m_dynamicsWorld->addRigidBody(body); @@ -923,6 +933,28 @@ void DemoApplication::renderme() BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); yStart += yIncr; +//enable BT_DEBUG_MEMORY_ALLOCATIONS define in Bullet/src/LinearMath/btAlignedAllocator.h for memory leak detection +#ifdef BT_DEBUG_MEMORY_ALLOCATIONS + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gTotalBytesAlignedAllocs = %d",gTotalBytesAlignedAllocs); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; +#endif //BT_DEBUG_MEMORY_ALLOCATIONS + + if (getDynamicsWorld()) + { + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"# objects = %d",getDynamicsWorld()->getNumCollisionObjects()); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"# pairs = %d",getDynamicsWorld()->getBroadphase()->getOverlappingPairCache()->getNumOverlappingPairs()); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + + } + + #endif //SHOW_NUM_DEEP_PENETRATIONS resetPerspectiveProjection(); diff --git a/Demos/OpenGL/DemoApplication.h b/Demos/OpenGL/DemoApplication.h index 7d93f3e53..0c4585b54 100644 --- a/Demos/OpenGL/DemoApplication.h +++ b/Demos/OpenGL/DemoApplication.h @@ -58,6 +58,8 @@ class DemoApplication ///constraint for mouse picking btTypedConstraint* m_pickConstraint; + btCollisionShape* m_shootBoxShape; + float m_cameraDistance; int m_debugMode; diff --git a/Extras/glui/GL/glui.h b/Extras/glui/GL/glui.h index cee21e25f..4b268066e 100644 --- a/Extras/glui/GL/glui.h +++ b/Extras/glui/GL/glui.h @@ -757,7 +757,7 @@ public: void *font; int curr_modifiers; - void adjust_glut_xy( int &x, int &y ) { x; y = h-y; } + void adjust_glut_xy( int &x, int &y ) { (void)x; y = h-y; } void activate_control( GLUI_Control *control, int how ); void align_controls( GLUI_Control *control ); void deactivate_current_control( void ); @@ -884,19 +884,19 @@ public: virtual int get_id( void ) const { return user_id; } virtual void set_id( int id ) { user_id=id; } - virtual int mouse_down_handler( int local_x, int local_y ) { local_x; local_y; return false; } - virtual int mouse_up_handler( int local_x, int local_y, bool inside ) { local_x; local_y; inside; return false; } - virtual int mouse_held_down_handler( int local_x, int local_y, bool inside) { local_x; local_y; inside; return false; } - virtual int key_handler( unsigned char key, int modifiers ) { key; modifiers; return false; } - virtual int special_handler( int key,int modifiers ) { key; modifiers; return false; } + virtual int mouse_down_handler( int local_x, int local_y ) { (void)local_x; (void)local_y; return false; } + virtual int mouse_up_handler( int local_x, int local_y, bool inside ) { (void)local_x; (void)local_y; (void)inside; return false; } + virtual int mouse_held_down_handler( int local_x, int local_y, bool inside) { (void)local_x; (void)local_y; (void)inside; return false; } + virtual int key_handler( unsigned char key, int modifiers ) { (void)key; (void)modifiers; return false; } + virtual int special_handler( int key,int modifiers ) { (void)key; (void)modifiers; return false; } virtual void update_size( void ) { } virtual void idle( void ) { } - virtual int mouse_over( int state, int x, int y ) { state; x; y; return false; } + virtual int mouse_over( int state, int x, int y ) { (void)state; (void)x; (void)y; return false; } virtual void enable( void ); virtual void disable( void ); - virtual void activate( int how ) { how; active = true; } + virtual void activate( int how ) { (void)how; active = true; } virtual void deactivate( void ) { active = false; } /** Hide (shrink into a rollout) and unhide (expose from a rollout) */ @@ -955,7 +955,7 @@ public: void sync_live( int recurse, int draw ); /* Reads live variable */ void init_live( void ); void output_live( int update_main_gfx ); /** Writes live variable **/ - virtual void set_text( const char *t ) { t; } + virtual void set_text( const char *t ) { (void)t; } void execute_callback( void ); void get_this_column_dims( int *col_x, int *col_y, int *col_w, int *col_h, diff --git a/src/BulletCollision/BroadphaseCollision/btAxisSweep3.h b/src/BulletCollision/BroadphaseCollision/btAxisSweep3.h index eb601ec7c..c7721aca4 100644 --- a/src/BulletCollision/BroadphaseCollision/btAxisSweep3.h +++ b/src/BulletCollision/BroadphaseCollision/btAxisSweep3.h @@ -288,6 +288,7 @@ btAxisSweep3Internal::~btAxisSweep3Internal() if (m_ownsPairCache) { + m_pairCache->~btOverlappingPairCache(); btAlignedFree(m_pairCache); } } diff --git a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp index ce05dc153..661270b9b 100644 --- a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp +++ b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp @@ -124,30 +124,52 @@ btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration() if (m_ownsStackAllocator) { m_stackAlloc->destroy(); + m_stackAlloc->~btStackAlloc(); btAlignedFree(m_stackAlloc); } if (m_ownsCollisionAlgorithmPool) { + m_collisionAlgorithmPool->~btPoolAllocator(); btAlignedFree(m_collisionAlgorithmPool); } if (m_ownsPersistentManifoldPool) { + m_persistentManifoldPool->~btPoolAllocator(); btAlignedFree(m_persistentManifoldPool); } + m_convexConvexCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_convexConvexCreateFunc); + + m_convexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_convexConcaveCreateFunc); + m_swappedConvexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_swappedConvexConcaveCreateFunc); + + m_compoundCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_compoundCreateFunc); + + m_swappedCompoundCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_swappedCompoundCreateFunc); + + m_emptyCreateFunc->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_emptyCreateFunc); + + m_sphereSphereCF->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_sphereSphereCF); + + m_sphereBoxCF->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_sphereBoxCF); + m_boxSphereCF->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_boxSphereCF); + m_sphereTriangleCF->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_sphereTriangleCF); + m_triangleSphereCF->~btCollisionAlgorithmCreateFunc(); btAlignedFree( m_triangleSphereCF); + m_simplexSolver->~btVoronoiSimplexSolver(); btAlignedFree(m_simplexSolver); + m_pdSolver->~btGjkEpaPenetrationDepthSolver(); btAlignedFree(m_pdSolver); diff --git a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp index d7f46c015..7d4ed7856 100644 --- a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp +++ b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp @@ -124,6 +124,10 @@ m_btSeed2(0) } } +btSequentialImpulseConstraintSolver::~btSequentialImpulseConstraintSolver() +{ + +} void initSolverBody(btSolverBody* solverBody, btRigidBody* rigidbody) { @@ -360,18 +364,14 @@ btScalar resolveSingleFrictionCacheFriendly( #endif //NO_FRICTION_TANGENTIALS -btAlignedObjectArray tmpSolverBodyPool; -btAlignedObjectArray tmpSolverConstraintPool; -btAlignedObjectArray tmpSolverFrictionConstraintPool; -btAlignedObjectArray gOrderTmpConstraintPool; -btAlignedObjectArray gOrderFrictionConstraintPool; -void addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation) + +void btSequentialImpulseConstraintSolver::addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation) { - btSolverConstraint& solverConstraint = tmpSolverFrictionConstraintPool.expand(); + btSolverConstraint& solverConstraint = m_tmpSolverFrictionConstraintPool.expand(); solverConstraint.m_contactNormal = normalAxis; solverConstraint.m_solverBodyIdA = solverBodyIdA; @@ -469,7 +469,7 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol //todo: use stack allocator for this temp memory int minReservation = numManifolds*2; - //tmpSolverBodyPool.reserve(minReservation); + //m_tmpSolverBodyPool.reserve(minReservation); //don't convert all bodies, only the one we need so solver the constraints /* @@ -480,8 +480,8 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol if (rb && (rb->getIslandTag() >= 0)) { btAssert(rb->getCompanionId() < 0); - int solverBodyId = tmpSolverBodyPool.size(); - btSolverBody& solverBody = tmpSolverBodyPool.expand(); + int solverBodyId = m_tmpSolverBodyPool.size(); + btSolverBody& solverBody = m_tmpSolverBodyPool.expand(); initSolverBody(&solverBody,rb); rb->setCompanionId(solverBodyId); } @@ -489,8 +489,8 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol } */ - //tmpSolverConstraintPool.reserve(minReservation); - //tmpSolverFrictionConstraintPool.reserve(minReservation); + //m_tmpSolverConstraintPool.reserve(minReservation); + //m_tmpSolverFrictionConstraintPool.reserve(minReservation); { int i; @@ -519,16 +519,16 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol solverBodyIdA = rb0->getCompanionId(); } else { - solverBodyIdA = tmpSolverBodyPool.size(); - btSolverBody& solverBody = tmpSolverBodyPool.expand(); + solverBodyIdA = m_tmpSolverBodyPool.size(); + btSolverBody& solverBody = m_tmpSolverBodyPool.expand(); initSolverBody(&solverBody,rb0); rb0->setCompanionId(solverBodyIdA); } } else { //create a static body - solverBodyIdA = tmpSolverBodyPool.size(); - btSolverBody& solverBody = tmpSolverBodyPool.expand(); + solverBodyIdA = m_tmpSolverBodyPool.size(); + btSolverBody& solverBody = m_tmpSolverBodyPool.expand(); initSolverBody(&solverBody,rb0); } @@ -539,16 +539,16 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol solverBodyIdB = rb1->getCompanionId(); } else { - solverBodyIdB = tmpSolverBodyPool.size(); - btSolverBody& solverBody = tmpSolverBodyPool.expand(); + solverBodyIdB = m_tmpSolverBodyPool.size(); + btSolverBody& solverBody = m_tmpSolverBodyPool.expand(); initSolverBody(&solverBody,rb1); rb1->setCompanionId(solverBodyIdB); } } else { //create a static body - solverBodyIdB = tmpSolverBodyPool.size(); - btSolverBody& solverBody = tmpSolverBodyPool.expand(); + solverBodyIdB = m_tmpSolverBodyPool.size(); + btSolverBody& solverBody = m_tmpSolverBodyPool.expand(); initSolverBody(&solverBody,rb1); } } @@ -580,10 +580,10 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol btScalar rel_vel; btVector3 vel; - int frictionIndex = tmpSolverConstraintPool.size(); + int frictionIndex = m_tmpSolverConstraintPool.size(); { - btSolverConstraint& solverConstraint = tmpSolverConstraintPool.expand(); + btSolverConstraint& solverConstraint = m_tmpSolverConstraintPool.expand(); solverConstraint.m_solverBodyIdA = solverBodyIdA; solverConstraint.m_solverBodyIdB = solverBodyIdB; @@ -690,21 +690,21 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol - int numConstraintPool = tmpSolverConstraintPool.size(); - int numFrictionPool = tmpSolverFrictionConstraintPool.size(); + int numConstraintPool = m_tmpSolverConstraintPool.size(); + int numFrictionPool = m_tmpSolverFrictionConstraintPool.size(); ///todo: use stack allocator for such temporarily memory, same for solver bodies/constraints - gOrderTmpConstraintPool.resize(numConstraintPool); - gOrderFrictionConstraintPool.resize(numFrictionPool); + m_orderTmpConstraintPool.resize(numConstraintPool); + m_orderFrictionConstraintPool.resize(numFrictionPool); { int i; for (i=0;igetRigidBodyA().getIslandTag() >= 0) && (constraint->getRigidBodyA().getCompanionId() >= 0)) { - tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].writebackVelocity(); + m_tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].writebackVelocity(); } if ((constraint->getRigidBodyB().getIslandTag() >= 0) && (constraint->getRigidBodyB().getCompanionId() >= 0)) { - tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].writebackVelocity(); + m_tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].writebackVelocity(); } constraint->solveConstraint(infoGlobal.m_timeStep); if ((constraint->getRigidBodyA().getIslandTag() >= 0) && (constraint->getRigidBodyA().getCompanionId() >= 0)) { - tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].readVelocity(); + m_tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].readVelocity(); } if ((constraint->getRigidBodyB().getIslandTag() >= 0) && (constraint->getRigidBodyB().getCompanionId() >= 0)) { - tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].readVelocity(); + m_tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].readVelocity(); } } { - int numPoolConstraints = tmpSolverConstraintPool.size(); + int numPoolConstraints = m_tmpSolverConstraintPool.size(); for (j=0;j m_tmpSolverBodyPool; + btAlignedObjectArray m_tmpSolverConstraintPool; + btAlignedObjectArray m_tmpSolverFrictionConstraintPool; + btAlignedObjectArray m_orderTmpConstraintPool; + btAlignedObjectArray m_orderFrictionConstraintPool; + + protected: btScalar solve(btRigidBody* body0,btRigidBody* body1, btManifoldPoint& cp, const btContactSolverInfo& info,int iter,btIDebugDraw* debugDrawer); btScalar solveFriction(btRigidBody* body0,btRigidBody* body1, btManifoldPoint& cp, const btContactSolverInfo& info,int iter,btIDebugDraw* debugDrawer); void prepareConstraints(btPersistentManifold* manifoldPtr, const btContactSolverInfo& info,btIDebugDraw* debugDrawer); + void addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation); ContactSolverFunc m_contactDispatch[MAX_CONTACT_SOLVER_TYPES][MAX_CONTACT_SOLVER_TYPES]; ContactSolverFunc m_frictionDispatch[MAX_CONTACT_SOLVER_TYPES][MAX_CONTACT_SOLVER_TYPES]; @@ -68,7 +77,7 @@ public: m_frictionDispatch[type0][type1] = func; } - virtual ~btSequentialImpulseConstraintSolver() {} + virtual ~btSequentialImpulseConstraintSolver(); virtual btScalar solveGroup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifold,int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,btDispatcher* dispatcher); diff --git a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp index 5d5cc5f84..d047474c8 100644 --- a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp +++ b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp @@ -65,7 +65,7 @@ m_gravity(0,-10,0), m_localTime(btScalar(1.)/btScalar(60.)), m_profileTimings(0) { - if (m_constraintSolver) + if (!m_constraintSolver) { void* mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16); m_constraintSolver = new (mem) btSequentialImpulseConstraintSolver; @@ -88,9 +88,16 @@ btDiscreteDynamicsWorld::~btDiscreteDynamicsWorld() { //only delete it when we created it if (m_ownsIslandManager) + { + m_islandManager->~btSimulationIslandManager(); btAlignedFree( m_islandManager); + } if (m_ownsConstraintSolver) - btAlignedFree(m_constraintSolver); + { + + m_constraintSolver->~btConstraintSolver(); + btAlignedFree(m_constraintSolver); + } } void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep) diff --git a/src/LinearMath/btAlignedAllocator.cpp b/src/LinearMath/btAlignedAllocator.cpp index 526ab3261..40bb7422a 100644 --- a/src/LinearMath/btAlignedAllocator.cpp +++ b/src/LinearMath/btAlignedAllocator.cpp @@ -17,6 +17,60 @@ subject to the following restrictions: int gNumAlignedAllocs = 0; int gNumAlignedFree = 0; +int gTotalBytesAlignedAllocs = 0;//detect memory leaks + +#ifdef BT_DEBUG_MEMORY_ALLOCATIONS +//this generic allocator provides the total allocated number of bytes +#include + +void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename) +{ + void *ret; + char *real; + unsigned long offset; + + gTotalBytesAlignedAllocs += size; + gNumAlignedAllocs++; + + printf("allocation#%d from %s,line %d, size %d\n",gNumAlignedAllocs,filename,line,size); + real = (char *)malloc(size + 2*sizeof(void *) + (alignment-1)); + if (real) { + offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) & +(alignment-1); + ret = (void *)((real + 2*sizeof(void *)) + offset); + *((void **)(ret)-1) = (void *)(real); + *((int*)(ret)-2) = size; + + } else { + ret = (void *)(real);//?? + } + int* ptr = (int*)ret; + *ptr = 12; + return (ret); +} +#include +void btAlignedFreeInternal (void* ptr,int line,char* filename) +{ + + void* real; + gNumAlignedFree++; + + if (ptr) { + real = *((void **)(ptr)-1); + int size = *((int*)(ptr)-2); + gTotalBytesAlignedAllocs -= size; + + printf("free #%d from %s,line %d, size %d\n",gNumAlignedFree,filename,line,size); + + free(real); + } else + { + printf("NULL ptr\n"); + } +} + +#else //BT_DEBUG_MEMORY_ALLOCATIONS + #if defined (BT_HAS_ALIGNED_ALLOCATOR) @@ -25,19 +79,19 @@ int gNumAlignedFree = 0; #include -void* btAlignedAlloc (size_t size, int alignment) +void* btAlignedAllocInternal (size_t size, int alignment) { gNumAlignedAllocs++; void* ptr = _aligned_malloc(size,alignment); -// printf("btAlignedAlloc %d, %x\n",size,ptr); +// printf("btAlignedAllocInternal %d, %x\n",size,ptr); return ptr; } -void btAlignedFree (void* ptr) +void btAlignedFreeInternal (void* ptr) { gNumAlignedFree++; -// printf("btAlignedFree %x\n",ptr); +// printf("btAlignedFreeInternal %x\n",ptr); _aligned_free(ptr); } @@ -49,13 +103,13 @@ void btAlignedFree (void* ptr) -void* btAlignedAlloc (size_t size, int alignment) +void* btAlignedAllocInternal (size_t size, int alignment) { gNumAlignedAllocs++; return memalign(alignment, size); } -void btAlignedFree (void* ptr) +void btAlignedFreeInternal (void* ptr) { gNumAlignedFree++; free(ptr); @@ -63,7 +117,7 @@ void btAlignedFree (void* ptr) #else -void* btAlignedAlloc (size_t size, int alignment) +void* btAlignedAllocInternal (size_t size, int alignment) { void *ret; char *real; @@ -82,7 +136,7 @@ void* btAlignedAlloc (size_t size, int alignment) return (ret); } -void btAlignedFree (void* ptr) +void btAlignedFreeInternal (void* ptr) { void* real; @@ -97,4 +151,5 @@ void btAlignedFree (void* ptr) #endif +#endif //BT_DEBUG_MEMORY_ALLOCATIONS diff --git a/src/LinearMath/btAlignedAllocator.h b/src/LinearMath/btAlignedAllocator.h index 82ce94926..38e90c49b 100644 --- a/src/LinearMath/btAlignedAllocator.h +++ b/src/LinearMath/btAlignedAllocator.h @@ -21,12 +21,26 @@ subject to the following restrictions: ///that is better portable and more predictable #include "btScalar.h" +//#define BT_DEBUG_MEMORY_ALLOCATIONS 1 +#ifdef BT_DEBUG_MEMORY_ALLOCATIONS -void* btAlignedAlloc (size_t size, int alignment); +#define btAlignedAlloc(a,b) \ + btAlignedAllocInternal(a,b,__LINE__,__FILE__) -void btAlignedFree (void* ptr); +#define btAlignedFree(ptr) \ + btAlignedFreeInternal(ptr,__LINE__,__FILE__) +void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename); +void btAlignedFreeInternal (void* ptr,int line,char* filename); + +#else + void* btAlignedAllocInternal (size_t size, int alignment); + void btAlignedFreeInternal (void* ptr); + + #define btAlignedAlloc(a,b) btAlignedAllocInternal(a,b) + #define btAlignedFree(ptr) btAlignedFreeInternal(ptr) +#endif typedef int size_type; diff --git a/src/LinearMath/btStackAlloc.h b/src/LinearMath/btStackAlloc.h index f60b24e41..ebdc43004 100644 --- a/src/LinearMath/btStackAlloc.h +++ b/src/LinearMath/btStackAlloc.h @@ -50,7 +50,7 @@ public: if(usedsize==0) { - if(!ischild) + if(!ischild && data) btAlignedFree(data); data = 0;